Repository navigation
A failed connect no longer crashes the process, and reconnects back off - #31
Merged
Merged
Conversation
…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.
|
All contributors have signed the @imqueue Contribution Terms. ✅ |
Member
Author
|
I have read the CLA Document and I hereby sign the CLA |
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.
zero-evoke
approved these changes
Nov 30, 2026
Mikhus
approved these changes
Nov 30, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
What does this PR do?
A failed (re)connect crashed the host process. In
pg8,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 realpgclient on a refused port: the old build exits with an unhandledECONNREFUSED, the new one rejectsconnect()and keeps retrying.onNotification,onNotificationLockExec), the re-listen after a reconnect (onReconnect), the re-listen on a lock release, the'connect'handler insideconnect()and the reconnect timer all report a failure through'error'.retryDelay(100 ms) and doubles on each failed attempt up to the newretryDelayMaxoption (default 10 s), and starts over after a successful connection. Before, a database that was down got ten attempts a second per process, forever.'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
Checklist
npm test).Contribution terms (required)
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.