Skip to content

v4 Scripts: build the NOSCRIPT retry from the rendered EVALSHA, not the caller's spans - #3398

Merged
mgravell merged 3 commits into
v4from
marc/v4-script-retry-capture
Dec 3, 2026
Merged

mgravell merged 3 commits into
v4from
marc/v4-script-retry-capture

Conversation

@mgravell

@mgravell mgravell commented Dec 3, 2026

Copy link
Copy Markdown
Member

Fixes the two Scripts.Evaluate issues raised in #3364 that don't need any new API.

Problem

When the SCRIPT LOAD + EVALSHA pair hadn't completed synchronously, Repairable captured keys.ToArray() / args.ToArray() in case it had to re-send behind EVAL after a NOSCRIPT. That caused two problems:

  1. Use after return. The copy is shallow: a RedisValue over Memory<byte> still points at the caller's buffer. The arguments are only borrowed for the duration of the call, so a caller may hand a pooled buffer back as soon as EvaluateAsync returns. If the server then replied NOSCRIPT (a SCRIPT FLUSH, a restart, a failover to a node that never saw the load), the retry sent whatever was in that buffer by then. No error is raised; the script just runs with the wrong arguments.
  2. Two allocations on almost every call. That path is taken whenever the reply isn't already there, which is nearly every real network call, so the arrays were paid for on every call, for a retry that almost never happens.

Change

  • SendWithPreambleAsync gains overloads (internal) that also hand back a reference of the caller's own to the detached request: a reference count, not a copy.

  • Repairable holds that reference instead of the spans. It releases it immediately on the synchronous-success fast path, and otherwise once the reply is in.

  • On NOSCRIPT, the new RenderEval reads the key count, keys and arguments back out of the rendered EVALSHA frame and renders EVAL/EVAL_RO the normal way:

    • through the command map, so a renamed or disabled EVAL is handled like any other command;
    • via WithoutKeyPrefix(), because the keys in the frame already carry the prefix;
    • with key marks and slot recomputed by the builder, rather than patched by hand.

    The copying happens only on the NOSCRIPT path.

Tests

RespScriptRetryArgumentsTests (new). The fake raises an error reply as RedisServerException, as the real executor does, and can hold the EVALSHA reply until the caller has moved on.

  • The caller's pooled buffer is overwritten after the call returns but before the NOSCRIPT arrives, and the retry still sends the original bytes. Covered for EVAL and EVAL_RO. On the current v4 code this test sends the overwritten bytes ($7|XXXXXXX|).
  • A synchronous NOSCRIPT is still repaired.
  • A key-prefixed context doesn't get the prefix twice on the retry, and the retry routes to the same slot.
  • Payloads that look like framing (CRLF, $, digits, empty), and the zero-key case.
  • With EVAL renamed in the command map, and with EVALSHA renamed to a name of a different length.

The new tests plus the existing script, preamble, NOSCRIPT, end-to-end and SanityChecks classes pass locally on net10.0 and net481, against the docker topology.

…caller's spans

The repair captured keys/args with ToArray() whenever the send had not completed synchronously. That copied the RedisValues but not the memory under them, so a caller who returned a pooled buffer after the call returned had the retry send whatever was there by the time NOSCRIPT arrived; and it cost two arrays on almost every real call.

The retry now holds a reference to the rendered EVALSHA (a reference count, no copy) and re-renders EVAL from its arguments through the command map, without re-applying the key prefix the keys already carry.
@mgravell mgravell mentioned this pull request Dec 3, 2026
…not defaulted

Internal, so correctness wins over convenience: a caller that forgets to pass its token should not compile.
@mgravell
mgravell merged commit 98ed98d into v4 Dec 3, 2026
3 checks passed
@mgravell
mgravell deleted the marc/v4-script-retry-capture branch December 3, 2026 15:34
pairbit added a commit to pairbit/IT.Redis that referenced this pull request Dec 3, 2026
* v4 design: generated scripts and functions (seredis#3401)

* design: generated scripts and functions (exploratory)

A shape for declaring server-side code as partial methods: binding by name, shared call shape, per-kind provisioning; facts checked against Redis 8.12.

* design: functions are opaque names; administration on IServer, not in the pipeline

* design: lists of keys and arguments, and how a script sees them

* design: the general case - declared commands, after the marc/respite RespCommandGenerator

* design: keyword clauses (prefix, suffix, count, ignore-when) as the core of the declared-command model

* v4: Redis 8.12 test topology (cluster bus protected mode, ZSCAN duplicates), from main's seredis#3343 (seredis#3399)

* CI (Windows): let Redis 8.12 start the cluster nodes

8.12 refuses a cluster node with an unauthenticated bus port unless cluster-bus-port-protected-mode is off; older servers reject the option, so it is passed only to 8.12 and later.

* Redis 8.12 test topology, ported from main's seredis#3343: cluster-bus-port-protected-mode in the cluster confs, client-libs-test:8.12.0, and ZSCAN may return duplicates

Replaces the version-gated CI.yml option: main already solves this in the cluster confs, and v4 follows it.

* v4 Scripts: build the NOSCRIPT retry from the rendered EVALSHA, not the caller's spans (seredis#3398)

* Scripts: build the NOSCRIPT retry from the rendered EVALSHA, not the caller's spans

The repair captured keys/args with ToArray() whenever the send had not completed synchronously. That copied the RedisValues but not the memory under them, so a caller who returned a pooled buffer after the call returned had the retry send whatever was there by the time NOSCRIPT arrived; and it cost two arrays on almost every real call.

The retry now holds a reference to the rendered EVALSHA (a reference count, no copy) and re-renders EVAL from its arguments through the command map, without re-applying the key prefix the keys already carry.

* SendWithPreambleAsync: the gate and cancellation token are required, not defaulted

Internal, so correctness wins over convenience: a caller that forgets to pass its token should not compile.

---------

Co-authored-by: Marc Gravell <marc.gravell@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.

1 participant