porsager / porsager/postgres

Error swap between completing and failing query in build step (with max connections = 1)

Open
#729 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

  • Tested node versions: v16.19.0, v20.9.0
  • Tested postgres.js versions: 3.0.6, 3.4.3
  • Tested postgres version: 14

Hi,
with the following conditions:

  1. the max number of connections is set to 1
  2. the connection is already open
  3. two queries are run at the same time

If the second query fails the build step, the build error is thrown on the first query, and the second query never resolves nor throws.

The bug occurs also for any n >= 2, as in the squared parenthesis in the comments.

const postgres = require("postgres");

(async () => {
  const sql = postgres({
    // important: max should be 1 [ n - 1 ]
    max: 1
  });
  try {
    // connection must exists. If this query is not run the bug does not occurs
    await sql`SELECT 2`;

    // 2 [n] queries. The first [first to (n-1)th] one is ok, but the second [nth] should fail in the build step
    await Promise.all([
      // this is a valid query and should resolve. It throws the error in the second query instead
      sql`SELECT 1`.catch((e) => {
        console.log(
          "Catched error from other query",
          e,
          e.query /* SELECT 1*/
        );
      }),
      // this query should fail *in the build step*. It never resolves/throws
      sql`SELECT ${undefined}`.catch((e) => {
        console.log("Never catched: this line won't be logged", e, e.query);
      }),
    ]);
    console.log("This line will never be logged");
  } catch (error) {
    console.log("This line will never be logged. Error in one promise: ", error);
    throw error;
  }
  await sql.end();
})().catch((e) => {
  console.error("Error", e);
});

I'm sorry, I tried but cannot suggest the fix.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Node.js reproduction with max connections set to 1 and inspect the connection scheduling and query build-step handling it exercises. Done means the valid concurrent query resolves, the invalid query rejects with its own error, and both promises settle without swapping errors or hanging.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.