Skip to content

[AI-4368] Keep a released call hold released in Redis - #2712

Open
kanat wants to merge 5 commits into
mainfrom
kanat/ai-4368-hold-release-races
Open

kanat wants to merge 5 commits into
mainfrom
kanat/ai-4368-hold-release-races

Conversation

@kanat

@kanat kanat commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

A call hold that a session had released could reappear in Redis, and another customer's session on the same call was then refused for up to 30 seconds. This happened when a renewal read the hold just before the release, or when a hold write that timed out still ran in Redis after the release. A release now leaves a "released" mark on the hold for 30 seconds, and the hold script never writes over that mark.

How it works

call hash node:call:<app>:<id>, one field per hold

field value        meaning                      liveCall (every script)
<until> acme       live hold of acme            counts as acme's hold
<until> acme, past lapsed hold                  deleted
-<until> acme      released, mark still live    skipped, nobody's hold
-<until> acme, past released, mark lapsed       deleted
sequenceDiagram
    participant Session as Session (node A)
    participant Renewal as Renewal tick (node A)
    participant Redis
    Renewal->>Renewal: copy the holds to renew
    Session->>Redis: release: HSET field "-<now+30s> acme"
    Renewal->>Redis: hold script for the same field
    Redis-->>Renewal: -1, the field is marked released, nothing written
    Note over Redis: the call is free for another customer at once
Loading

Tickets: AI-4368 (renewal after release) and AI-4369 (timed-out hold after release).

A hold write that timed out and reaches Redis after the release meets the same mark and gets the same -1.

Change Result Test
A release writes -<now+holdFor> <customer> into its own field and sets the key to expire in holdFor The mark lasts as long as a hold would, and a release of a hold that never landed still leaves a key that expires TestAHoldThatRunsAfterItsReleaseStaysReleased
The hold script answers -1 and writes nothing when its own field holds a live mark A renewal or a late hold cannot bring a released hold back TestAHoldReleasedDuringARenewalStaysReleased, TestAHoldThatRunsAfterItsReleaseStaysReleased
liveCall never counts a mark as a holder, and deletes it once it lapses Releasing still frees the call at once, and marks do not pile up TestACallIsHeldUntilTheLastSessionOnItIsGone and the other release tests, TestAHoldDropsWhatLapsed
Only the hold's own field is checked, before the script walks the call, so a late renewal is told -1 even when another customer has the call since Another session's hold on the same call renews exactly as before (values taken from the base) TestAHoldBesideAReleasedOneRenewsAsBefore
A node's hold counter starts at a random number A node that restarts at the same address does not reuse the name of a hold its previous run released TestANodeThatRestartedHoldsACallItsLastRunReleased
Before After
Release, then a renewal that read the hold first the hold is back for up to 30 s the hold stays released
Release, then a hold that timed out lands the hold is back for up to 30 s the hold stays released
A live hold renewed <now+30s> acme, key TTL 30 s unchanged

Why a mark and not "renew only what is still there"

A renewal that only refreshes fields still present would close the first race, but not the second: a timed-out first hold also creates its field, so only something the release leaves behind can refuse it. It would also break a promise the code makes today: a hold that Redis never got is written by the next renewal (TestAHoldThatWasNotSaidIsSaidWithTheNextRenewal). One mark, checked by the one script that writes holds, closes both races and keeps that promise.

Rolling deploy

A router of the previous release reads -<until> as a lapsed hold and deletes it, as tonumber gives a negative number. While old and new routers share Redis, a mark can therefore be removed early by an old router. That is the old behaviour, not a new failure: old routers already release with a plain delete.

Mutations, commands and runs

Base repro (tests only, on d734ca0): TestAHoldReleasedDuringARenewalStaysReleased and TestAHoldThatRunsAfterItsReleaseStaysReleased fail with "Should be empty, but was acme". The control tests (TestAHoldBesideAReleasedOneRenewsAsBefore, TestANodeThatRestartedHoldsACallItsLastRunReleased, TestAHoldDropsWhatLapsed) pass on the base.

Mutations of acceleration/internal/node/directory.go, all killed (one script, each applied, tested and restored in turn):

Mutant Killed by
liveCall deletes live marks too (else HDEL) ReleasedDuringARenewal, RunsAfterItsRelease
liveCall never deletes lapsed marks AHoldDropsWhatLapsed
A live mark counts as a holder 7 release tests
Hold script refusal removed ReleasedDuringARenewal, RunsAfterItsRelease
Hold script refuses when any field of the call is marked BesideAReleasedOne, NodeThatRestarted
Refusal answers 0 instead of -1 (renewal logs "another customer's") ReleasedDuringARenewal
Release deletes the field instead of marking it ReleasedDuringARenewal, RunsAfterItsRelease
The mark lapses at once (now instead of now + holdFor) ReleasedDuringARenewal, RunsAfterItsRelease
Release sets no expiry on the key RunsAfterItsRelease
Hold counter starts at 0 NodeThatRestarted
Release not retryable ACallIsReleasedThoughTheConnectionBrokeUnderIt
The mark lapses at half of holdFor (now + holdFor/2 in the release script) TestAReleasedHoldStaysMarkedUntilItWouldHaveLapsed
liveCall drops marks 15 s before they lapse TestAReleasedHoldStaysMarkedUntilItWouldHaveLapsed
The hold script checks its own mark after the walk instead of before it TestALateRenewalOfAReleasedHoldIsToldItWasReleasedWhenAnotherCustomerHasTheCall

The seven hold and release tests are TestAHoldDropsWhatLapsed, TestAHoldReleasedDuringARenewalStaysReleased, TestAHoldThatRunsAfterItsReleaseStaysReleased, TestAReleasedHoldStaysMarkedUntilItWouldHaveLapsed, TestALateRenewalOfAReleasedHoldIsToldItWasReleasedWhenAnotherCustomerHasTheCall, TestAHoldBesideAReleasedOneRenewsAsBefore and TestANodeThatRestartedHoldsACallItsLastRunReleased.

Commands, in acceleration/, with ROUTER_REDIS_ADDR set to a separate Redis per run:

  • go test -tags integration ./internal/node/ -count=1 and once more with -race: pass
  • the seven tests above with -count=50 -cpu 1 and with -race: pass
  • go test -tags integration ./internal/session/ -run TestCallHoldSuite and ./internal/api/ -run 'TestPhoneSuite|TestCallStatusSuite': pass
  • gofmt -l, go vet ./internal/node/, go vet -tags integration ./internal/node/: clean

Review rounds: the main reviewer gave NO-GO in round 1, the security review gave GO, and round 2 gave GO.

🤖 Generated with Claude Code

kanat and others added 3 commits November 29, 2026 12:11
A release now marks its field as released for holdFor instead of
deleting it, and the hold script refuses to write over that mark, so a
renewal or a timed-out hold that reaches Redis after the release cannot
bring the hold back (AI-4368, AI-4369).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text review: 5 unclear of 5

The four comments are hard to read cold (T1.1, a Should fix since there are three or more). The PR opening is a nit (T1.2).

[Should fix] T1.1 comment, outside the diff hunk: directory.go:358-360 (only line 360 changed). The sentence ends with a "which would leave" clause, so I read it twice. Suggested:

// The release is retryable because a hold that timed out breaks the client's connection, and the next command then fails without being sent.
// Without the retry the call stays held for up to holdFor, and another customer's session with the same id is refused. Sending it twice does no harm.

[Nit] T1.2 PR opening: it has four sentences, and the third is long. "Hold" and "field" are not explained. Suggested:

A call hold that a session had released could reappear in Redis, and another customer's session on the same call was then refused for up to 30 seconds. This happened when a renewal read the hold just before the release, or when a hold write that timed out still ran in Redis after the release. A release now leaves a "released" mark on the hold for 30 seconds, and the hold script never writes over that mark.

The ticket links can go after the chart.

Comment thread acceleration/internal/node/directory.go Resolved
Comment thread acceleration/internal/node/directory.go Resolved
Comment thread acceleration/internal/node/directory.go Resolved

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[security R1] VERDICT: GO for 59c6f7c.

No findings. What I checked:

  • A released hold stays released inside the hold's lifetime: a renewal or a late hold that reaches Redis after the release is refused, and sending the release twice only rewrites the mark.
  • A release marks only the hold its own session made. A mark never refuses another customer's session, and a live hold beside a mark still does.
  • The holds guard whose call an id is. The calls-at-once limit counts call legs in Postgres (internal/dlc/limits.go:98), so it does not depend on them.
  • A router of the previous release reads a mark as no holder and deletes it, so a rollout or a rollback falls back to the base behaviour and opens nothing new.
  • A release never shortens the hash's expiry under another live hold.
  • No internal error text reaches a caller or a stored field.

Each new guard was removed in turn, and the PR's tests failed every time (8 of 8). The new order-dependent tests pass 50 times each with the default flags, with -cpu 1 and with -race.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: NO-GO

Reviewed 59c6f7c on the current main (d734ca0, the PR's merge base). The design closes both races: a release leaves a mark on its own field, the hold script refuses to write over a live mark, and a new hold always gets a new field, so the mark never blocks a later hold of the same call. One Should fix remains: no test pins how long the mark lasts (R1.1).

Checked and sound

  • The two new race tests fail on the base with "Should be empty, but was acme". The controls (BesideAReleasedOne, NodeThatRestarted, DropsWhatLapsed) pass on the base and on the head.
  • A live hold renews exactly as on the base: same value, same key TTL, and lapsed holds are still deleted.
  • Each script is one EVAL, so it runs atomically. Release and hold both set the key to expire holdFor after Redis's own time, so the key never expires before a live hold lapses.
  • Rolling deploy, probed with the base Lua: a previous-release script deletes a mark as lapsed, and a late renewal after that brings the hold back. This is what the PR body describes, and it is what happens on every release today. A previous-release hold for another customer right after a new release succeeds. A previous-release live hold beside a new release keeps renewing.
  • Rollback: marks left behind read as empty on the base scripts, which delete them.
  • Growth: 300 hold/release cycles on one call leave 300 marks for 30 s, and the first read after they lapse removes all of them.
  • 18 mutants: 14 killed. R4 (a mark without its customer) is equivalent, since nothing reads a mark's customer. R5 is the order change in R1.2. R6 and R7 are R1.1.
  • go test -tags integration -race ./internal/node/, ./internal/session/ -run TestCallHoldSuite and ./internal/api/ -run 'TestPhoneSuite|TestCallStatusSuite' pass. The PR is a draft, so go and integration CI run at ready.

Verdict: request changes, for R1.1.

Comment thread acceleration/internal/node/directory.go Resolved
Comment thread acceleration/internal/node/directory.go Resolved
kanat and others added 2 commits November 29, 2026 12:38
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…reword the comments

A late renewal of a released hold answered 0 once another customer had taken the call, so the
renewal logged a refusal by another customer. The hold script now checks the field's mark before
it walks the call.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

Reviewed 011067d on base d734ca0 (main has nothing newer).

Prior findings

  • R1.1 fixed. I applied the two mutants again: a mark that lapses after holdFor/2, and a liveCall that drops marks 15 s early. Both now fail TestAReleasedHoldStaysMarkedUntilItWouldHaveLapsed; both passed before.
  • R1.2 fixed. The hold script now checks its own field's mark before the walk, in the same EVAL as the walk and the write, so it stays atomic. A live hold is never matched by the check, so renewals write the same value and TTL (TestAHoldBesideAReleasedOneRenewsAsBefore passes). Moving the check back after the walk fails the new test (expected -1, got 0).
  • T1.1 fixed. I checked each rewritten comment against the code (counter and random start, mark skipped for every customer, old routers delete a negative value, -1 before the walk, retryable release): all true.
  • T1.2 fixed. The opening and the "own field" row are right.

Runs

  • Node integration suite with -race, TestCallHoldSuite, TestPhoneSuite|TestCallStatusSuite: pass. The seven hold/release tests with -count=50 -cpu 1 and with -race -count=50: pass. gofmt and vet clean.
  • 20 mutants of the hold/release code (the author's M1-M11 and mine): all killed except three equivalent ones (a refusal for an already lapsed mark, a refusal limited to the mark's own customer, a no-op piece in the holder script).
  • CI on the draft: labeler and GitGuardian pass, go and integration skipped, CodeQL pending.

New finding

  • [Nit] PR body details block: "the five new or changed tests" and the 11-row mutation table are out of date (seven tests now, and the mark-lapse, early-drop and late-renewal rows are missing). Refresh it before merge.

@kanat
kanat marked this pull request as ready for review November 29, 2026 17:54

This branch has not been deployed

No deployments
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