Repository navigation
fix(sentinel): reject connect() instead of hanging when the resolved master is unreachable - #3331
Merged
nkaradzhov merged 1 commit intoJul 13, 2026
Conversation
…master is unreachable Fixes #3066. When sentinel discovery succeeds but the resolved master is unreachable (e.g. a TLS misconfiguration, or a firewalled address), the master and replica clients keep the caller's reconnectStrategy - which by default retries forever - so their connect() promises never settle. transform() awaited them indefinitely, #connect()'s existing retry/give-up loop was never reached, and the public connect() hung with nothing to observe. Make the failure observable by racing each master/replica connect() against the client's first 'error' event (already emitted per failed attempt by the socket). A failed attempt now feeds #connect()'s retry loop, which re-discovers the topology each round - picking up a new master address mid-failover - and, if the initial connect never succeeds, gives up after maxCommandRediscovers attempts so connect() rejects with the underlying error. This mirrors the fail-fast behavior sentinel discovery clients already have via reconnectStrategy: false. On that final failure the internal connect() now tears everything down (clients, pub/sub proxy) so nothing keeps reconnecting in the background after the rejection, and the topology-scan timer is no longer started for a connect() that failed. Per-attempt errors are still surfaced through the existing 'client-error' event (and 'error' with passthroughClientErrorEvents), unchanged. The redundant console.log in #connect()'s catch is removed; the trace line above it already records the failure.
GiHoon1123
force-pushed
the
fix-3066-sentinel-unreachable-master-hang
branch
from
July 12, 2026 04:15
5023b48 to
04aac8f
Compare
2 of 3 tasks
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 #3066. Follow-up to #3329, implementing the approach suggested in its closing review: making initial master connection failure observable to
#connect()soconnect()rejects instead of hanging.Root cause. When sentinel discovery succeeds but the resolved master is unreachable (a TLS misconfiguration in the original report; equally a firewalled/NATed address), the master clients keep the caller's
reconnectStrategy— which by default retries forever — so theirconnect()promises never settle.transform()awaits them inPromise.allindefinitely,#connect()'s existing retry/give-up loop is never reached (nothing throws), and the publicconnect()hangs with nothing to observe, catch, or time out on.Fix.
#connectOrFail()races each master/replicaconnect()against that client's first'error'event — which the socket already emits once per failed attempt. This provides both kinds of awareness the review asked for:connectTimeoutsurface as aConnectionTimeoutErrorthrough the same'error'event and reject the same way (exercised by the TLS regression test below).A rejected round feeds
#connect()'s existing retry loop, which re-discovers the topology each round — picking up a newly promoted master mid-failover, arguably more correct than retrying a possibly-stale address inside the client — and, while not yet ready, gives up aftermaxCommandRediscoversattempts soconnect()rejects with the underlying socket error. This mirrors the fail-fast behavior sentinel discovery clients already have viareconnectStrategy: false; the retry policy for the initial connect lives in the (already configurable) outer loop rather than inside each node client. An overall initial-connect timeout was considered instead, but it would need a new tunable and would delay detection of immediately-observable failures by a full window per round — happy to rework toward that shape if you'd prefer it.Teardown on final failure. Between rounds,
transform()'s existing "destroy old clients before opening new ones" step disposes of the previous round's still-retrying clients. On the final failure there is no next round, so the internalconnect()now tears everything down (destroy()) before rethrowing — after the rejection nothing keeps reconnecting in the background, no timers hold the event loop open, and the instance is back in a closed state from whichconnect()can be retried. The topology-scan timer is also no longer started for aconnect()that failed (previously it was started unconditionally in thefinally, keeping the event loop alive while#reset()no-ops when not ready).Scope: replicas included. Replica connections are awaited in the same
Promise.alland previously hung the exact same way when a replica was unreachable, so the same one-line treatment is applied there. This does not make replicas "more required" than before — the initial connect always awaited them; the change only converts an unbounded hang into the same bounded retry/reject behavior.Known limitation (unchanged behavior). A server that accepts the TCP connection but never responds to the handshake produces no
'error'event and is not covered here — that is the province of the existingsocketTimeoutoption, and standalonecreateClient()behaves identically today.console.log(e)in#connect()'s catch is removed rather than converted to an'error'emission (per the review of #3329, an unguarded emit in the retry loop can throwERR_UNHANDLED_ERROR): the trace line above it already records the failure, per-attempt errors still flow through the existing'client-error'event (and'error'withpassthroughClientErrorEvents), and the terminal failure is now the rejection itself.Tests. Two docker-backed regression tests, both asserting
connect()rejects and the instance is fully closed afterwards:nodeAddressMapremaps only the resolved master to an unreachable address, so sentinel discovery stays healthy throughout (immediate-refusal path). Also asserts noclient-errorevents arrive after the rejection, i.e. nothing is left reconnecting in the background.nodeClientOptions: { socket: { tls: true } }against the plaintext test topology — reproducing the exact failure mode reported in createSentinel promis doesn't reslove #3066 (sentinel connects fine, master clients fail TLS) with no certificate setup, and exercising the per-attemptconnectTimeoutpath.Checklist
npm testpass with this change (including linting)? —tsc --buildand lint are clean; the full docker-based sentinel suite passes locally on macOS (93 passing, 1 pending) using the Docker Desktop host-networking setup documented in docs(contributing): document Docker Desktop host networking requireme… #3330.connect()rejecting on failure is the already-documented contract.Note
Medium Risk
Changes initial Sentinel connection and teardown behavior on failure; bounded risk to connect/topology paths but affects a critical entry point for all Sentinel users.
Overview
Fixes #3066: when Sentinel discovery works but master/replica nodes cannot be reached,
connect()now rejects instead of hanging forever.#connectOrFailwraps each master/replicaconnect()so the first socketerrorrejects immediately (defaultreconnectStrategywould otherwise leave those promises pending). Failures feed the existing#connect()rediscover/retry loop and, aftermaxCommandRediscovers, surface as a rejected publicconnect().On final connect failure, the client **
destroy()**s background node clients, leavesisOpenfalse, and the topology scan timer only starts when connect actually succeeded. A strayconsole.login the connect retry path is removed.Docker regression tests cover an unreachable remapped master and TLS-to-plaintext master misconfiguration.
Reviewed by Cursor Bugbot for commit 04aac8f. Bugbot is set up for automated code reviews on this repo. Configure here.