Skip to content

A failed connect no longer crashes the process, and reconnects back off - #31

Merged
Mikhus merged 2 commits into
masterfrom
reconnect-without-crashing
Nov 30, 2026
Merged

Mikhus merged 2 commits into
masterfrom
reconnect-without-crashing

Conversation

@SerhiyGreench

@SerhiyGreench SerhiyGreench commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

A failed (re)connect crashed the host process. In pg 8, Client.connect() without a callback returns a promise and reports a failure to connect only through it — it emits no 'error' on the client. PgPubSub.connect() left that promise unhandled, so a refused socket (ECONNREFUSED) or a database starting up or shutting down (57P03 the database system is shutting down) was an unhandled rejection and Node exited. connect() itself never settled either, because it waited for an 'error' event that never came. A database restart took down every process listening on it at once.

Now:

  • connect() rejects with the error the database or network gave. Reconnecting still goes on in the background (re-armed by the client's 'end', as before), and each failed background attempt is reported through 'error', or the logger when nothing listens. Verified against a real pg client on a refused port: the old build exits with an unhandled ECONNREFUSED, the new one rejects connect() and keeps retrying.
  • No other async listener can leak a rejection: the notification handlers (onNotification, onNotificationLockExec), the re-listen after a reconnect (onReconnect), the re-listen on a lock release, the 'connect' handler inside connect() and the reconnect timer all report a failure through 'error'.
  • Reconnects back off. The delay starts at retryDelay (100 ms) and doubles on each failed attempt up to the new retryDelayMax option (default 10 s), and starts over after a successful connection. Before, a database that was down got ten attempts a second per process, forever.
  • The 'reconnect' event still carries the number of attempts it took.

Tests: a refused first connect rejects connect(), leaves no unhandled rejection and reports every reconnect attempt; delays grow 10, 20, 40, 40… (test constants) and start over after a reconnect; a reconnect re-listens every channel on the new client. README updated. Adds an option, so this is meant to ship as 3.1.0.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / internal
  • Other:

Checklist

  • I have read the Contributing guide.
  • Tests added or updated, and the full suite passes locally (npm test).
  • Docs / doc-blocks updated where relevant.
  • The PR is focused on a single logical change.

Contribution terms (required)

  • I have read and agree to the @imqueue Contribution Terms.
    I grant the project owner the right to license my contribution
    commercially, royalty-free, my contribution stays available under
    GPL-3.0, I keep my copyright, and I understand I will receive no fee for
    it. If I did not agree, I would not be submitting this contribution.

…onnects back off

pg 8 reports a failure to connect only through the promise Client.connect()
returns, never as an 'error' event. connect() left that promise unhandled, so
a refused socket or a database starting up or shutting down (57P03) was an
unhandled rejection that ended the host process, and connect() itself never
settled. The rejection now goes down the same failure path: connect()
rejects with it, and a background reconnect attempt reports it through
'error' (or the logger).

The other async listeners can no longer leak a rejection either: the
notification handlers, the re-listen after a reconnect, the lock release
re-listen and the reconnect timer all report failures through 'error'.

Reconnects back off: the delay starts at retryDelay and doubles on each
failed attempt up to the new retryDelayMax option (10 s by default), and
starts over once connected, instead of ten attempts a second forever.
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

All contributors have signed the @imqueue Contribution Terms. ✅
Posted by the CLA Assistant Lite bot.

@SerhiyGreench

Copy link
Copy Markdown
Member Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 30, 2026
recreateClient() replaced reListenChannels with the keys of the locks it
then deleted. After one failed reconnect attempt there were no locks left,
so the next attempt replaced the list with nothing, and the reconnect that
finally succeeded re-listened no channel: after an outage longer than one
retry the process stopped hearing notifications for good ("re-subscribing
0 channel(s)"). The list is now merged with the remaining locks instead of
replaced.
@SerhiyGreench
SerhiyGreench requested a review from Mikhus November 30, 2026 20:07
@Mikhus
Mikhus merged commit f554338 into master Nov 30, 2026
14 checks passed
@Mikhus
Mikhus deleted the reconnect-without-crashing branch November 30, 2026 20:11
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants