porsager / porsager/postgres

bug: pool never recovers after releasing a reserved connection whose backend was terminated.

Open
#1,199 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

Hey 👋

Thanks for making JavaScript a better place! 🫶

While working on query cancellation in kysely-postgres-js I stumbled upon this behavior:

  1. create a pool of 1.
  2. reserve a connection with reserve().
  3. run a long query on it, e.g. pg_sleep.
  4. terminate the connection's backend in a secondary session via select pg_terminate_backend(pid).
  5. the inflight query rejects with CONNECTION_CLOSED as expected.
  6. release the reserved connection back to the pool with release().
  7. run another query on the same pool. it never settles.
  8. we get an uncaught exception:
    TypeError: Cannot read properties of null (reading 'write')
    	at Immediate.nextWrite (.../postgres/src/connection.js:255:22)
    

The pool discarding the dead connection and using another (existing or new) seems like the wanted behavior in this scenario.
Skipping the release(), allows next queries to run.

Possibly related: #1195, #1186.

Happens everytime when using postgres@3.4.9, Node.js v26.1.0, PostgreSQL 18.4.

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

Reproduce the seven-step scenario with postgres@3.4.9, Node.js v26.1.0, and PostgreSQL 18.4, then inspect postgres/src/connection.js around line 255 and the pool reserve/release path. Done means releasing a reserved connection whose backend was terminated no longer leaves the pool hung or raises the null-write exception, and a subsequent query settles.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgres, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.