Skip to content

fix(client): keep writing on drain before the socket is ready - #3591

Merged
nkaradzhov merged 1 commit into
redis:masterfrom
nkaradzhov:fix/drain-write-before-ready
Nov 10, 2026
Merged

nkaradzhov merged 1 commit into
redis:masterfrom
nkaradzhov:fix/drain-write-before-ready

Conversation

@nkaradzhov

@nkaradzhov nkaradzhov commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This pull request fixes #3584.

After a reconnect, a client resubscribes with one SSUBSCRIBE per sharded channel. With enough channels, these commands fill the socket buffer, so the write stops and waits for drain. The drain handler went through #scheduleWrite(), which did nothing until the socket was ready. 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.

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. drain calls 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

  • Does npm test pass with this change (including linting)? (@redis/client suite and lint pass; other packages not run locally.)
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)? (No API change.)

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 on drain, but the handler waited for socket.isReady—which only happens after all subscribe replies—so the rest of the queue never flushed.

Separates scheduling from readiness. #scheduleWrite() now only dedupes setImmediate and is what drain, ready, and #maybeScheduleWrite() call; #maybeScheduleWrite() gates on isReady and 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 parallel SETs, massive sharded resubscribe after CLIENT KILL, ordering so PING does not hit the wire before HELLO when 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.

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T16:28:20.898024Z 7419130 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@nkaradzhov
nkaradzhov merged commit 6dbcb59 into redis:master Nov 10, 2026
15 checks passed
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>
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.

Sharded pub/sub resubscribe after reconnect stalls forever when the SSUBSCRIBE commands exceed the socket's writableHighWaterMark

2 participants