Skip to content

[FIXED in 4.3.0] A query that fails to serialize gets another query's error and response #20

Closed

Description

@okomarov

A query that throws while it is built or serialized (too many parameters, a value that fails to serialize, a throwing debug callback) is handled as if it failed on the server. When another query is already running on the connection, the wrong query gets the error and later queries receive each other's responses. This is porsager#1082 and porsager#729, and it returns wrong results, not only a hang.

Reproduction

PostgreSQL 17, main at 032ea2b:

const sql = postgres({ ...opts, max: 1 })
await sql`select 1`

const a = sql`select pg_sleep(0.2) as a`.execute()
const b = sql.unsafe('select 1', Array(70000).fill(1)).execute()

await new Promise(r => setTimeout(r, 500))
const c = sql`select 2 as c`.execute()
  1. a rejects with MAX_PARAMETERS_EXCEEDED, which is b's error.
  2. b hangs. It was never written to the server.
  3. c resolves with [] instead of [{ c: 2 }], because it received the response meant for another query. Only then does b fail, with ERR_OUT_OF_RANGE.

Cause

submit() in src/connection.js registers the query as active (query) or pipelined (sent) before build() and toBuffer() run. When either throws, the catch:

  • calls errored(error), which rejects the active query rather than the one that failed;
  • writes a Sync only when nothing is pipelined, so a failing query in sent has no messages on the wire, yet it still waits for a response. Every later response is then matched to the query before it.

On an idle connection, the same path happens to work: the failing query is the active one, and the Sync gives it the ReadyForQuery it waits for.

Fix

Keep the failing query as a placeholder in the pipeline, always write a Sync for it, and reject that query, not the active one. Each placeholder then consumes exactly one ReadyForQuery, so responses stay matched in order. The placeholder also returns an idle connection to the pool, as it does today.

Tests

First item in the correctness batch on #5, independent of #18.

Activity

  1. added a commit that references this issue on Oct 11, 2026
  2. moved this to Shipped in Roadmapon Oct 15, 2026
  3. added
    area: protocol
    bug
    on Oct 16, 2026
  4. changed the title [-]A query that fails to serialize gets another query's error and response[/-] [+][FIXED in 4.3.0] A query that fails to serialize gets another query's error and response[/+] on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: protocolbug

    Type

    No type

    Projects

    • Status
      Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions