Skip to content

when a transaction is interrupted, Pool error BadResource: Bad resource ID #385

Open
@iugo

Description

@iugo

Use the following code to test, there is a certain probability of problems. (使用以下代码测试, 有一定概率出现问题)

import * as postgres from 'https://deno.land/x/[email protected]/mod.ts';

const pool2 = new postgres.Pool(databaseUrl, 2, true);

Deno.test('pool-error', async () => {
  const p1 = (async () => {
    const c = await pool2.connect();
    console.log('p1', c.connected);
    try {
      const t = c.createTransaction('p1-t');
      await t.begin();
      t.queryArray(
        `INSERT INTO test_table ("at") VALUES ($1);`,
        [new Date().toString() + 'p1'],
      );
      throw new Error('some error');
    } finally {
      c.release();
    }
  })().catch((err) => {
    console.error('p1', err);
  });

  const p2 = (async () => {
    const c = await pool2.connect();
    console.log('p2', c.connected);
    try {
      const t = c.createTransaction('p2-t');
      await t.begin();
      t.queryArray(
        `INSERT INTO test_table ("at") VALUES ($1);`,
        [new Date().toString() + 'p2'],
      );
      await t.commit();
    } finally {
      c.release();
    }
  })().catch((err) => {
    console.error('p2', err);
  });

  try {
    await Promise.all([p1, p2]);
  } finally {
    await pool2.end();
  }
});

The error info is: (出现的错误为)

BadResource: Bad resource ID
      rr = await this.rd.read(this.buf);
           ^
    at async read (deno:ext/net/01_net.js:24:19)
    at async BufReader.read (https://deno.land/[email protected]/io/buffer.ts:383:12)
    at async BufReader.readFull (https://deno.land/[email protected]/io/buffer.ts:415:20)
    at async Connection.#readMessage (https://deno.land/x/[email protected]/connection/connection.ts:152:5)
    at async Connection.#preparedQuery (https://deno.land/x/[email protected]/connection/connection.ts:853:27)
    at async Connection.query (https://deno.land/x/[email protected]/connection/connection.ts:930:16)
    at async Transaction.queryArray (https://deno.land/x/[email protected]/query/transaction.ts:413:14)

This problem does not occur if catch (err) { await t.rollback(); throw err; } is done in the above code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions