Repository navigation
Don't release the listening client twice when stopping during LISTEN - #668
Open
joshuavivo wants to merge 2 commits into
Open
joshuavivo wants to merge 2 commits into
joshuavivo wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()inlistenForChangesremoves theerrorlistener but leaveserrorHandledfalse. If the runner stops while theLISTENis still in flight and that query then fails, its rejection handler callsrelease()again and schedules a reconnect. The secondreleaseClient()makes pg-pool throwRelease called on client which has already been released to the pool.as an unhandled rejection. SettingerrorHandled = trueat the top ofrelease()stops both; the existing TODO inrelease()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 nounhandledRejectionlistener, main exits with code 1 afterrunner.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: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 thaterrorHandledalso gates.connecthook on the test's own pool, not by patchingpg.Connection.prototype): expects onepool:listen:release, nopool: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
yarn lint:fixpasses.yarn testpasses. (Locally on Node 22.23.1 / PostgreSQL 16: 24/24 suites, 109/109 tests.)RELEASE_NOTES.mdfile (if one exists). (Changeset added instead.)Written with AI assistance (Claude ran the repros and tests and drafted this).