Skip to content

fix: clear socketTimeout timer on close so it can't destroy the reconnected socket - #62

Merged
mcollina merged 1 commit into
valkey-io:mainfrom
webdevelopersrinu:fix/socket-timeout-stale-timer
Jul 13, 2026
Merged

mcollina merged 1 commit into
valkey-io:mainfrom
webdevelopersrinu:fix/socket-timeout-stale-timer

Conversation

@webdevelopersrinu

Copy link
Copy Markdown
Contributor

Problem

When socketTimeout is set, the timer armed by setSocketTimeout() is only ever
cleared inside its own stream.once("data") handler. If the socket the timer was
armed for closes before any reply arrives (e.g. the connection drops with a
command in flight), that "data" handler never fires, so the timer is never cleared.

The timer callback runs this.stream.destroy(...) — and by the time it fires,
this.stream points to the new socket created by the reconnect. So a healthy,
freshly-reconnected socket is destroyed with a spurious
Socket timeout. Expecting data... error, triggering yet another reconnect.

There's a second effect: because a new timer is only armed when
socketTimeoutTimer === undefined, the stale timer also prevents the reconnected
socket from being monitored until it fires.

Reproduction (socketTimeout: 1000): send a command, drop the socket before any
reply, let the client reconnect — ~1000ms after the original command the stale timer
fires and destroys the healthy socket.

Fix

Add Redis#clearSocketTimeout() and call it from closeHandler, which runs on every
socket close (wired via stream.once("close", ...)) before any reconnect. This cancels
the stale timer so the next command re-arms a fresh one on the current socket.

Tests

Added a functional regression test that arms the timer, drops the socket to force a
reconnect, and asserts the healthy reconnected socket is not destroyed. Verified it
fails on main and passes with this fix. Existing socketTimeout tests still pass.

…nected socket

The socketTimeout timer is only cleared inside setSocketTimeout()'s own
stream.once("data") handler. If the socket closes before any reply arrives
(e.g. the connection drops with a command in flight), that handler never
runs, so the timer survives the reconnect. When it fires it runs
this.stream.destroy(...) against the new, healthy stream, causing a spurious
"Socket timeout" disconnect. Until it fires, the reconnected socket is also
never re-monitored, because a new timer is only armed when
socketTimeoutTimer === undefined.

Add Redis#clearSocketTimeout() and call it from closeHandler, which runs on
every socket close before any reconnect, so a dead socket's timer can't
affect a later connection and the next command re-arms a fresh timer.

Adds a functional regression test that fails without this change.

Signed-off-by: webdevelopersrinu <[email protected]>
@webdevelopersrinu
webdevelopersrinu force-pushed the fix/socket-timeout-stale-timer branch from d6a46f0 to 21b21df Compare July 12, 2026 13:28
@mcollina
mcollina merged commit deec948 into valkey-io:main Jul 13, 2026
1 check passed
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.

2 participants