Skip to content

Don't release the listening client twice when stopping during LISTEN - #668

Open
joshuavivo wants to merge 2 commits into
graphile:mainfrom
joshuavivo:fix-665-listen-double-release
Open

joshuavivo wants to merge 2 commits into
graphile:mainfrom
joshuavivo:fix-665-listen-double-release

Conversation

@joshuavivo

Copy link
Copy Markdown

Description

Fixes #665. The diagnosis and the one-line fix are from that issue, credit to its reporter; this PR adds tests and a changeset.

release() in listenForChanges removes the error listener but leaves errorHandled false. If the runner stops while the LISTEN is still in flight and that query then fails, its rejection handler calls release() again and schedules a reconnect. The second releaseClient() makes pg-pool throw Release called on client which has already been released to the pool. as an unhandled rejection. Setting errorHandled = true at the top of release() stops both; the existing TODO in release() already says we shouldn't try again at that point.

With the #665 repro (LISTEN held back on the wire, then runner.stop(), then the backends terminated) and no unhandledRejection listener, main exits with code 1 after runner.stop() resolves, 20/20 runs; with this change it exits 0, 20/20. The window is narrow in practice (the reporter saw it about once in 150 CI runs), but when it hits, Node's default handling kills the process.

Tests, in __tests__/main.listenClient.test.ts:

  1. Listener's backend terminated with no stop in progress: one pool:listen:error, a reconnect on a new backend, and a job picked up via NOTIFY only. Passes on main too; it guards the reconnect path that errorHandled also gates.
  2. Stop while the LISTEN is in flight (held via a connect hook on the test's own pool, not by patching pg.Connection.prototype): expects one pool:listen:release, no pool:listen:error, no unhandled rejection. Fails on main every run (Expected: 1, Received: 2), passes with the fix (20/20 on Node 26.3.0, 10/10 on Node 22.23.1).

Performance impact

None; one boolean assignment per listening-client release.

Security impact

None.

Checklist

  • My code matches the project's code style and yarn lint:fix passes.
  • I've added tests for the new feature, and yarn test passes. (Locally on Node 22.23.1 / PostgreSQL 16: 24/24 suites, 109/109 tests.)
  • I have detailed the new feature in the relevant documentation. (Bug fix, nothing to document.)
  • I have added this feature to 'Pending' in the RELEASE_NOTES.md file (if one exists). (Changeset added instead.)
  • If this is a breaking change I've explained why. (Not breaking.)

Written with AI assistance (Claude ran the repros and tests and drafted this).

release() removed the `error` listener but left `errorHandled` false, so
when the in-flight LISTEN failed after a stop, its rejection handler
called release() again (and scheduled a reconnect). The second
releaseClient() made pg-pool throw "Release called on client which has
already been released to the pool." as an unhandled rejection.

Fixes graphile#665
processID isn't on the PoolClient type, so tsc -p tsconfig.test.json failed
on the new test file (TS2339 at three call sites).

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Listening client is released twice when the runner stops during an in-flight LISTEN

1 participant