Skip to content

fix: Client#end callback being called multiple times when first is no-op#3676

Merged
brianc merged 3 commits into
brianc:masterfrom
charmander:client-end-multi-callback-fix
May 14, 2026
Merged

fix: Client#end callback being called multiple times when first is no-op#3676
brianc merged 3 commits into
brianc:masterfrom
charmander:client-end-multi-callback-fix

Conversation

@charmander
Copy link
Copy Markdown
Collaborator

No description provided.

…no-op

(and unwanted retained listener even when not called multiple times)
…pre-connect behaviour closer to pg

As usual, the native client is extra full of bugs and inconsistencies, so this is just “good enough”.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a bug where Client#end's callback could be invoked multiple times when the first call was a no-op (e.g., calling end() before ever connecting). In packages/pg/lib/client.js, after invoking the user callback in the no-op branch the function now returns immediately, preventing fall-through into the regular end logic that would attach the same callback to the connection's end event. The native client (packages/pg/lib/native/client.js) is adjusted so that the deferred end scheduled on connect no longer re-invokes the original user callback (it uses a no-op), and it now only defers when actively connecting. A regression test is added.

Changes:

  • Add early return after invoking the no-op cb() in Client#end (packages/pg/lib/client.js).
  • Restrict deferred end-on-connect to the _connecting && !_connected case and decouple the deferred call from the user callback in the native client.
  • Add an integration test verifying that the no-op Client#end callback is fired exactly once.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/pg/lib/client.js Adds return after no-op callback invocation to prevent double-callback fall-through.
packages/pg/lib/native/client.js Tightens deferred-end condition to _connecting && !_connected and decouples scheduled re-end from the original callback.
packages/pg/test/integration/client/api-tests.js New integration test asserting the no-op end callback is invoked exactly once.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@charmander charmander requested a review from brianc May 14, 2026 05:41
Copy link
Copy Markdown
Owner

@brianc brianc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh nice

@brianc brianc merged commit fa47e73 into brianc:master May 14, 2026
7 checks passed
@charmander charmander deleted the client-end-multi-callback-fix branch May 14, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants