Skip to content

fix(sentinel): reject connect() instead of hanging when the resolved master is unreachable - #3331

Merged
nkaradzhov merged 1 commit into
redis:masterfrom
GiHoon1123:fix-3066-sentinel-unreachable-master-hang
Jul 13, 2026
Merged

nkaradzhov merged 1 commit into
redis:masterfrom
GiHoon1123:fix-3066-sentinel-unreachable-master-hang

Conversation

@GiHoon1123

@GiHoon1123 GiHoon1123 commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3066. Follow-up to #3329, implementing the approach suggested in its closing review: making initial master connection failure observable to #connect() so connect() 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 their connect() promises never settle. transform() awaits them in Promise.all indefinitely, #connect()'s existing retry/give-up loop is never reached (nothing throws), and the public connect() hangs with nothing to observe, catch, or time out on.

Fix. #connectOrFail() races each master/replica connect() 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:

  • failure awareness: immediately-observable failures (ECONNREFUSED, TLS handshake) reject within milliseconds;
  • timeout awareness: attempts that hit connectTimeout surface as a ConnectionTimeoutError through 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 after maxCommandRediscovers attempts so connect() rejects with the underlying socket error. This mirrors the fail-fast behavior sentinel discovery clients already have via reconnectStrategy: 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 internal connect() 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 which connect() can be retried. The topology-scan timer is also no longer started for a connect() that failed (previously it was started unconditionally in the finally, keeping the event loop alive while #reset() no-ops when not ready).

Scope: replicas included. Replica connections are awaited in the same Promise.all and 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 existing socketTimeout option, and standalone createClient() 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 throw ERR_UNHANDLED_ERROR): the trace line above it already records the failure, per-attempt errors still flow through the existing 'client-error' event (and 'error' with passthroughClientErrorEvents), 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:

  1. nodeAddressMap remaps only the resolved master to an unreachable address, so sentinel discovery stays healthy throughout (immediate-refusal path). Also asserts no client-error events arrive after the rejection, i.e. nothing is left reconnecting in the background.
  2. 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-attempt connectTimeout path.

Checklist

  • Does npm test pass with this change (including linting)? — tsc --build and 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.
  • Is the new or changed code fully tested? — two regression tests added and verified passing, plus the full sentinel suite for regressions.
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)? — N/A: no API change; 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.

#connectOrFail wraps each master/replica connect() so the first socket error rejects immediately (default reconnectStrategy would otherwise leave those promises pending). Failures feed the existing #connect() rediscover/retry loop and, after maxCommandRediscovers, surface as a rejected public connect().

On final connect failure, the client **destroy()**s background node clients, leaves isOpen false, and the topology scan timer only starts when connect actually succeeded. A stray console.log in 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.

…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
GiHoon1123 force-pushed the fix-3066-sentinel-unreachable-master-hang branch from 5023b48 to 04aac8f Compare July 12, 2026 04:15

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

CLGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createSentinel promis doesn't reslove

2 participants