Repository navigation
fix(client): keep writing on drain before the socket is ready - #3591
Merged
Merged
Conversation
After a reconnect, a client resubscribes with one SSUBSCRIBE per sharded channel. When those commands fill the socket buffer, the write stops and waits for `drain`. The drain handler went through the same ready check as new commands. The socket becomes ready only after every SSUBSCRIBE has a reply, so the rest of the commands were never sent and the client never became ready. Drain now continues the write without the ready check. New commands keep the check, now in #maybeScheduleWrite, so they still wait for the handshake. Fixes redis#3584
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
2 of 3 tasks
PavelPashov
approved these changes
Nov 9, 2026
2 of 3 tasks
nkaradzhov
added a commit
that referenced
this pull request
Nov 16, 2026
#3591) (#3622) Backport of 6dbcb59 to v5.12, for #3617. v5.12 resubscribes sharded channels with one SSUBSCRIBE each, like 6.x, and stalls the same way when they don't fit in one socket write: the drain handler went through the ready check, and the socket becomes ready only after every SSUBSCRIBE has a reply, so the rest were never sent. The change to index.ts is the same as on master. The cherry-pick conflicted only on surrounding code. In the tests, 5.x defaults to RESP2, so the handshake check looks for AUTH instead of HELLO. Co-authored-by: Nikolay Karadzhov <nkaradzhov89@gmail.com>
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
This pull request fixes #3584.
After a reconnect, a client resubscribes with one
SSUBSCRIBEper sharded channel. With enough channels, these commands fill the socket buffer, so the write stops and waits fordrain. Thedrainhandler went through#scheduleWrite(), which did nothing until the socket was ready. The socket becomes ready only after everySSUBSCRIBEhas a reply. So the rest of the commands were never sent, and the client never became ready.The fix separates the two checks:
#maybeScheduleWrite()now holds the ready check. Every caller that adds new work uses it, so new commands still wait for the handshake.#scheduleWrite()only makes sure that one write is scheduled at a time.draincalls it directly, because a drain only continues a write that already started._unpause()keeps the ready check. It can run while the handshake waits for credentials, before AUTH is queued.Supersedes #3586.
Checklist
npm testpass with this change (including linting)? (@redis/clientsuite and lint pass; other packages not run locally.)Note
Medium Risk
Touches core socket write scheduling for all commands; behavior change is intentional but could affect edge cases around handshake, pause, and backpressure.
Overview
Fixes stalled writes when the TCP buffer fills during reconnect/handshake. Large resubscribe batches (e.g. many
SSUBSCRIBEs) could block ondrain, but the handler waited forsocket.isReady—which only happens after all subscribe replies—so the rest of the queue never flushed.Separates scheduling from readiness.
#scheduleWrite()now only dedupessetImmediateand is whatdrain,ready, and#maybeScheduleWrite()call;#maybeScheduleWrite()gates onisReadyand pending queue work. Command enqueue paths (sendCommand, pub/sub helpers, pipeline/multi, MONITOR, reset, QUIT) use#maybeScheduleWrite()so new commands still wait for handshake completion.Regression coverage in
writing to the socket: huge parallelSETs, massive sharded resubscribe afterCLIENT KILL, ordering soPINGdoes not hit the wire beforeHELLOwhen credentials resolve asynchronously, and flush after_unpause().Reviewed by Cursor Bugbot for commit 7419130. Bugbot is set up for automated code reviews on this repo. Configure here.