Repository navigation
perf(client): replace per-command AbortSignal timeouts with deadline lists (backport #3618) - #3629
Conversation
…lists (backport redis#3618) Every command with a timeout created its own AbortSignal.timeout() and listener, which costs up to half the throughput under load and hundreds of MB of memory. The queue now keeps deadlines in CommandDeadlines: one list per timeout value with one timer per list. The client stops the timers when the socket ends and no command is left. Behavior and errors are unchanged. The cherry-pick conflicts in commands-queue.ts, so the queue changes are applied by hand to the 5.x queue: - A command moved to another queue (cluster slot migration) takes its deadline with it, so it times out in its new queue. - An abort ends the command's deadline, and a timeout removes its abort listener, so the second one no longer removes the node again. commands-queue.spec.ts holds only the tests that redis#3618 added. (cherry picked from commit 2f1e7d9)
Codex Review Summary: ✅ CompletedThis 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5f0d249964
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| }; | ||
| value.slotNumber = options?.slotNumber; | ||
|
|
||
| const node = this.#toWrite.add(value, options?.asap); |
There was a problem hiding this comment.
Validate timeouts before enqueueing ASAP commands
With a command already waiting to be written, submitting { asap: true, timeout: -1 } now inserts a new head before validation and removes it in the catch. DoublyLinkedList.remove() does not reset the remaining head's previous link, so if the original command is subsequently aborted or times out, the list is left with a stale tail. Adding and draining a subsequent PING then yields no command, leaving valid requests hanging. This sequence works on the parent because invalid timeouts are rejected before enqueueing. Validate first or fully restore the links on rollback.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 5f0d249. Configure here.
| current.value.slotNumber !== undefined && | ||
| slots.has(current.value.slotNumber) | ||
| ) { | ||
| CommandDeadlines.detach(current.value); |
There was a problem hiding this comment.
Detached commands hang without adopt
Medium Severity
extractCommandsForSlots and extractAllCommands now detach a command's deadline before the move. detach takes the entry off the source timer and does not fire it later. Cluster prepends with optional chaining, so a missing destination client skips adoptAll. Those commands leave every queue with no timer, so the promises never time out or settle. Previously each command kept its own AbortSignal.timeout() and still rejected.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 5f0d249. Configure here.


Description
Backports #3618 to
v5.12.On 5.12, every command with a
timeoutcreates its ownAbortSignal.timeout()and listener. Under load this costs up to half the throughput and hundreds of MB of memory. 5.x has no default command timeout, so this affects clients that settimeoutand commands queued during a maintenance timeout.command-deadlines.ts, its spec, and the'end'handler inindex.tsare the same as on master.git cherry-pick 2f1e7d9conflicts incommands-queue.ts, because the 5.x queue has no batches, preludes or#rejectCommand, so I applied the queue changes by hand:CommandDeadlinesinstead of anAbortSignal. It ends when the command is written, fails to encode, or is flushed.flushAll()and the socket's'end'stop the timers.extractCommandsForSlots/extractAllCommands, thenprependCommandsToWrite) takes its deadline with it, so it times out in its new queue. Without this, the old client's'end'would clear the timers that the moved commands depend on.5.12 has no
commands-queue.spec.ts, so I added one with only the tests from #3618. One change: the merge test moves the commands withextractCommandsForSlots, becauseextractAllCommandson 5.12 moves only the first command (it readscurrent.nextafterremove()cleared it). That bug is out of scope here.I ran
packages/clientwith Node 25 against the default test image: 2182 passing, 4 pending, 1 failing. The failure isLATENCY RESET, where the server logged a latency event right after the reset. It passes on its own three times in a row.Checklist
npm testpass with this change (including linting)? (packages/client, see above;v5.12has no lint script,npm run buildpasses)Note
Medium Risk
Changes when and how write-queue timeouts fire (including maintenance and cross-queue moves), though behavior is covered by new tests and matches prior AbortSignal validation.
Overview
Introduces
CommandDeadlines, a shared timer-per-timeout-value scheduler that replaces creating anAbortSignal.timeout()(and listener) for every queued command.RedisCommandsQueuenow trackstimerEntry/effectiveTimeout/wasInMaintenance, arms deadlines only while a command waits to write, and clears them on write, encode failure, abort, or flush.Moved commands (
extractCommandsForSlots/extractAllCommands→prependCommandsToWrite) keep their original expiry viadetach/adoptAll.flushAllandclearDeadlinesIfEmpty(also on socketend) stop timers so closed clients are not kept alive. Maintenance timeout resets go through the same#setDeadlinepath and still yieldCommandTimeoutDuringMaintenanceErrorwhen appropriate.Adds
command-deadlines.spec.tsand a newcommands-queue.spec.tsfocused on timeout and merge behavior.Reviewed by Cursor Bugbot for commit 5f0d249. Bugbot is set up for automated code reviews on this repo. Configure here.