Repository navigation
feat(client): add experimental rejectInFlight to time out and abort sent commands - #3636
Conversation
…Flight) Add the experimental `rejectInFlight` command option. With it, `timeout` and `abortSignal` also apply after a command is written, until its reply arrives. Such a command rejects with the new `TimeoutReadError` or `AbortReadError`: the server may have run it. Its entry stays in the read queue and its late reply is dropped, so the connection stays open and later replies stay matched. - Commands flagged `blocking` in the command metadata keep only the write-queue timeout; abort still applies to them. - MULTI and pipelines keep one timer until their last reply. If it fires after part of the batch is written, every pending command rejects with the read error and the rest of the batch is still written. - The maintenance timeout also relaxes in-flight timers, including a batch's, and expires with `CommandTimeoutReadDuringMaintenanceError`. - The multi-db default error filter skips `AbortReadError`. - The command `TimeoutError` now states the timeout in its message. Default `false`: without the flag, behavior is unchanged.
With rejectInFlight, a single blocking command drops its timer once it is written, because the server keeps blocking after a client-side timeout. A pipeline did not: its batch timer kept running, so a pipeline that held a blocking command rejected with TimeoutReadError. Commands in a pipeline block on the server, unlike commands inside MULTI. Drop the timer when the first command of such a pipeline is written. The abort signal still applies, and MULTI keeps its timer.
…Error TimeoutError means the command was never sent and is safe to retry. Two paths rejected commands that were already written with it: - A cluster slot migration that stops waiting for the replies of the source node now rejects them with TimeoutReadError. - SocketTimeoutDuringMaintenanceError now extends TimeoutReadError. BREAKING CHANGE: neither error is an instance of TimeoutError any more. The server may have executed the command: retry only if it is idempotent.
…egory TimeoutError and TimeoutReadError, with their maintenance subclasses, now map to the network category. Before, only the maintenance write timeouts did, and plain and read timeouts fell through to other. Abort errors stay in other.
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. |
The blocking-command tests set a 100 ms timeout with rejectInFlight in clientOptions, so it also applied to the harness's FLUSHALL before and after each test. On a slow runner FLUSHALL took longer, failed with TimeoutReadError, skipped client.destroy(), and the leaked client then failed the root after-all hook when the server stopped. Set the options with withCommandOptions on the call under test instead.
When several callers read the same cached key at once, they share the first caller's pending load. If that caller aborts, every waiter used to reject with the first caller's AbortError or AbortReadError, although none of them aborted. Waiters now drop the dead entry and load again, sharing one new load. Other errors are still shared.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 013b163e5a
ℹ️ 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".
| if (err instanceof AbortError || err instanceof AbortReadError) { | ||
| if (cacheEntry.validate()) this.delete(cacheKey); | ||
| return this.handleCache(client, parser, fn, transformReply, typeMapping, isStorable); |
There was a problem hiding this comment.
Skip cache reloads for callers whose own signal is aborted
When concurrent cache-coalesced calls share an AbortSignal, cancelling it aborts every waiter, not just the load's owner. This branch nevertheless retries each waiter: the first replacement load immediately fails addCommand's already-aborted-signal check, and all remaining waiters recursively retry that failure again. This makes bulk cancellation quadratic: a real-client reproduction with 1,001 coalesced GETs performed 501,501 handleCache calls and stalled the event loop for roughly half a second. The regression also affects queued commands without rejectInFlight. Check the waiter's own cancellation state before retrying the shared load.
Useful? React with 👍 / 👎.
Cdineshreddy12
left a comment
There was a problem hiding this comment.
Read this with the slot-migration interactions in mind, since the tombstone semantics ("stays in the read queue, reply discarded on arrival") touch the same positional-pairing invariants as #3642. Three notes:
The tombstone × waitForInflightCommandsToComplete interplay is coherent — worth stating in a comment. A rejected-in-flight command keeps #waitingForReply non-empty until its reply actually arrives, so during an SMIGRATED pause the bounded wait can now be extended by commands whose callers already got their read error. That's correct: the queue genuinely isn't drained, and if the 5s flush fires, the source connection is dropped (#3642), so the discarded replies die with the socket instead of desyncing anything. One small cleanup it suggests: #flushWaitingForReply now re-rejects entries that are already rejected — benign promise-wise, but skipping them would keep the flush's error/telemetry counts meaning "callers actually affected".
The node: undefined deadline safety is an invariant spread across three members — consider tying it with an assert. #onTimeout only dereferences node! on the not-in-flight path, and that's safe today because both #relaxInFlight call sites guarantee #isInFlight(head) (read-queue entries are written; the #toWrite site requires head.written || #isBatchStarted(head), and either disjunct satisfies #isInFlight). But the proof lives in the conjunction of #relaxInFlight's guards, #isInFlight's definition, and #onTimeout's branch order — a one-line assert (or a comment naming the invariant) at #setDeadline(undefined, …) would keep a future caller from quietly breaking it.
For the record, the interplay with #3647 (if both land) converges safely: a discarded reply can under-inform the shared script cache — an EVAL that actually ran isn't marked (costs one extra upload later), and a discarded NOSCRIPT doesn't unmark (the next surfaced failure does). Both err in the safe direction, so nothing needed — just noting it was checked.
The docs paragraph on batches ("pending commands reject, the rest of the batch is still sent") and the blocking-command carve-out both read exactly right to me.
# Conflicts: # packages/client/lib/client/commands-queue.ts
A rejectInFlight command that already rejected stays in the read queue to keep reply order. A flush now skips it instead of rejecting it again.
|
The red |
This pull request adds an experimental
rejectInFlightcommand option. Today,timeoutandabortSignalstop working once a command is written to the socket. If the server is slow to reply, the call waits for as long as the reply takes. WithrejectInFlight: true, they keep working until the reply arrives.When a sent command times out or is aborted, it rejects with the new
TimeoutReadErrororAbortReadError. These errors mean that the server may have run the command, so retry it only if it is idempotent. The connection stays open: the command keeps its place in the reply order, and its reply is dropped when it arrives. The option is off by default. Without it, nothing changes. It works per call or as a default incommandOptionsfor a client, cluster, sentinel or pool.How it works:
BLPOP,XREAD,WAIT, ...) keep only the write-queue timeout, because the server keeps blocking after a client-side timeout.abortSignalstill applies to them. A pipeline that holds a blocking command does the same. MULTI does not, because commands inside MULTI don't block. The client finds blocking commands through theblockingflag in the command metadata, which is exported asisBlocking.MULTI.CommandTimeoutReadDuringMaintenanceError.AbortReadError, just likeAbortError.networkerror category:TimeoutError,TimeoutReadErrorand their maintenance subclasses.TimeoutErrornow states the timeout in its message.Breaking change (minor).
TimeoutErrornow always means "never sent, safe to retry". Two rare paths used to reject commands that were already sent with it:SMIGRATED) stops waiting for the source node's replies after 5 s, those commands now reject withTimeoutReadError.SocketTimeoutDuringMaintenanceErrornow extendsTimeoutReadError. It only fires when maintenance notifications are on and the socket stays silent for the relaxed maintenance timeout.This should not affect real users:
err instanceof TimeoutErroron these errors. The error messages and theSocketTimeoutDuringMaintenanceErrorclass name are unchanged.Known limitations, documented in
docs/command-options.md:SELECTthe client's tracked database may not match the server's. Repeat the command, or turn the option off for that call.Note
High Risk
Changes core command-queue timeout/abort semantics and error types for commands that may have executed on the server; incorrect retry logic after
TimeoutReadErrorcould cause data corruption.Overview
Adds an experimental
commandOptions.rejectInFlightflag (defaultfalse) sotimeoutandabortSignalkeep applying while a command is already on the wire, not only while it waits in the write queue.With the flag on, slow or aborted in-flight calls reject with new
TimeoutReadError/AbortReadError(reply dropped, connection kept); queued-but-unsent commands still useTimeoutError/AbortError. The command queue was reworked to hold deadlines and abort listeners past write, handle MULTI/pipeline batches as a unit, skip read timeouts for blocking commands (via newisBlockingmetadata), and extend maintenance relaxed timeouts to in-flight commands.exec/execAsPipelinenow forward the option.Minor breaking change:
TimeoutErroris narrowed to “never sent”; cluster migration flush andSocketTimeoutDuringMaintenanceErrornow surfaceTimeoutReadErrorinstead. Related updates: client-side cache retries shared loads when the first caller aborts; multi-dbdefaultErrorFilterignoresAbortReadError; OTel classifies command timeouts viaTimeoutError/TimeoutReadError. Docs describe behavior, error matrix, and caveats.Reviewed by Cursor Bugbot for commit 7ad55ff. Bugbot is set up for automated code reviews on this repo. Configure here.