Skip to content

harness: compare-and-set for the affinity fallback, so concurrent past-grace turns of one session cannot split it #447

Open

Description

@pdettori

Follow-up from P6.3 (#425, slice 3 of #426). Spec §4 names this as a known gap: docs/specs/2026-10-04-p6-on-kubernetes-slice3-design.md, the paragraph after step 6.

Problem

Take two concurrent turns of one session that both find the session's affine sandbox gone past the grace period, or re-tiered. Each turn falls back in its tier and picks the least-loaded sandbox. The two can pick different sandboxes, A and B. Each then overwrites the affinity entry with a plain replace, a SET in RedisAffinityStore.replace (harness/src/sandbox-affinity.ts, the fallback branch of selectPoolSandbox in harness/src/select-sandbox.ts), and the later write wins.

Both turns report a workspace_reset, which is correct: the old workspace is gone either way. But only one of them, say B's, ends up in the entry. The session's next turn goes to B, so whatever the turn on A wrote is lost without a second reset notice. "Two concurrent turns of one session share one workspace" holds for first turns (the claim converge path) but not for fallbacks.

The window is narrow. It needs two in-flight turns of one session at the moment its sandbox's grace expires, so it most likely shows up after a long relay or worker outage with a client retrying in parallel.

Proposed fix

Make the fallback write a compare-and-set: a small Lua script that replaces the entry only if it still names the abandoned sandbox (prior.sandboxId). The loser then does what a losing first-turn claim already does. It releases its lease and re-runs selection once against the winner's entry. A second disagreement throws SandboxAffinityPendingError. The same CAS fits the cross-tier refresh replace in the affine-hit branch.

Acceptance

  • A unit test with a fake store: two past-grace selections of one session interleave, and both return the same sandbox, or one returns it and the other a pending 503.
  • An integration test over real Redis (beside packages/sandbox-relay/test/tier-affinity.integration.test.ts): two concurrent past-grace turns of one session never end on different sandboxes.
  • Spec §4's "Closing that race is a follow-up" sentence is replaced by the converge rule.

Activity

  1. added
    enhancement
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions