Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
iugo opened this issue Apr 14, 2022 · 1 comment
Open

Comments

@iugo
Copy link
Contributor

iugo commented Apr 14, 2022

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.

@Soremwar
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants