denodrivers / postgres

PostgreSQL driver for Deno

Home Page:https://denodrivers.github.io/postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

iugo opened this issue · comments

commented

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

import * as postgres from 'https://deno.land/x/postgres@v0.15.0/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/std@0.114.0/io/buffer.ts:383:12)
    at async BufReader.readFull (https://deno.land/std@0.114.0/io/buffer.ts:415:20)
    at async Connection.#readMessage (https://deno.land/x/postgres@v0.15.0/connection/connection.ts:152:5)
    at async Connection.#preparedQuery (https://deno.land/x/postgres@v0.15.0/connection/connection.ts:853:27)
    at async Connection.query (https://deno.land/x/postgres@v0.15.0/connection/connection.ts:930:16)
    at async Transaction.queryArray (https://deno.land/x/postgres@v0.15.0/query/transaction.ts:413:14)

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

Can you try with master to see if the error is still happening?