Repository navigation
fix: clear socketTimeout timer on close so it can't destroy the reconnected socket - #62
Merged
mcollina merged 1 commit intoJul 13, 2026
Conversation
…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
force-pushed
the
fix/socket-timeout-stale-timer
branch
from
July 12, 2026 13:28
d6a46f0 to
21b21df
Compare
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.
Problem
When
socketTimeoutis set, the timer armed bysetSocketTimeout()is only evercleared inside its own
stream.once("data")handler. If the socket the timer wasarmed 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.streampoints 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 reconnectedsocket from being monitored until it fires.
Reproduction (
socketTimeout: 1000): send a command, drop the socket before anyreply, 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 fromcloseHandler, which runs on everysocket close (wired via
stream.once("close", ...)) before any reconnect. This cancelsthe 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
mainand passes with this fix. ExistingsocketTimeouttests still pass.