Skip to content

[Bug]:Same-isolation-key concurrent calls race on sandbox state slot #2845

Open

Description

@larry-zy

Description

On a single HarnessAgent, two concurrent calls that resolve to the same SandboxIsolationKey operate on the
same persisted sandbox state record. The sandbox acquire → resume → persist → release window runs outside
the delegate's serializeOnKey gate, and the sandbox-layer guard defaults to noop() — so there is no
serialisation.

Both calls therefore start independent containers from the same state, mutate them in parallel, and
overwrite each other on completion. Workspace changes from one call are silently lost (last write wins),
with a transient double-container in between.

Expected behaviour

Concurrent calls resolving to the same isolation key should serialise and hand off state sequentially. No
call's workspace changes should be clobbered by a concurrent peer.

Actual behaviour

Calls run fully in parallel on the shared state slot; whichever call persists last wins, and the other
call's changes are dropped.

Steps to reproduce

  1. Build a HarnessAgent with the default scope (or explicit GLOBAL / AGENT).
  2. Fire two concurrent calls that resolve to the same isolation key, each mutating the sandbox workspace
    (e.g. writing a file).
  3. Inspect the final persisted state — one call's changes have been overwritten and lost.

Affected scopes

Any two concurrent calls that resolve to the same persisted state slot:

  • SESSION — same sessionId
  • USER (default) — same userId
  • AGENT — same agentId (fixed at build time)
  • GLOBAL — constant key "global", shared by all calls (easiest to hit)

Notes

Possible direction (non-binding)

Serialise same-key concurrent calls at the sandbox layer — e.g. a JVM-local default guard keyed by
SandboxIsolationKey. Multi-instance deployments would need a distributed guard (Redis SET NX, etc.).
Implementation details TBD in the PR.

Activity

  1. added
    bug
    on Aug 26, 2026
  2. oss-maintainer commented on Sep 24, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug (area/harness, sandbox lifecycle) | Status: Confirmed | Severity: high (silent data loss)

    Verified against main @ 3c1c29c01 by engine-assisted static review (paths under agentscope-harness/src/main/java/io/agentscope/harness/).

    Claim-by-claim check

    Claim Result Evidence
    C1 — one persisted sandbox state slot per isolation key TRUE agent/sandbox/SessionSandboxStateStore.java:44-92 (slotSessionId maps SESSION/USER/AGENT/GLOBAL); SandboxManager.java:214-252 persists after each call
    C2 — the sandbox-layer guard defaults to noop, so same-key calls run unserialised TRUE agent/sandbox/SandboxExecutionGuard.java:78-93 (noop() default); wired only when a DistributedStore/explicit guard is configured (HarnessAgent.java:2470-2473, :2396-2399)
    C3 — acquire→resume→persist→release runs outside the delegate's serializeOnKey gate TRUE HarnessAgent.java:942-957 wraps delegate.call in Mono.using; the gate lives inside AgentBase.java:256-262,291,374-397 and is keyed on (userId, sessionId) (ReActAgent.java:727-737) — coarser sandbox keys (USER/AGENT/GLOBAL) are not protected even in one instance
    C4 — both calls resume independent containers from the same state and the later persist wins TRUE SandboxManager.java:100-121 (no compare-on-load); SessionSandboxStateStore.java:58-65 → plain full-replacement save (AgentStateStore.java:69-79); saveIfVersion CAS exists at AgentStateStore.java:87 but is unused here; workspace snapshot likewise overwritten on stop() (AbstractBaseSandbox.java:65-96,132-141)
    C5 — not covered by the #2490 fix TRUE agent/middleware/SandboxLifecycleMiddleware.java:54,114-121,155-164 — per-call binding + compare-and-clear, i.e. the different-key live-binding race only
    Already fixed on main? NO No in-process per-key guard exists on main; only external-store guards (Redis/JDBC/Mongo/ControlPlane). Default configuration is reproducible

    Root cause. The only per-sandbox-key serialization seam (SandboxExecutionGuard) is optional and defaults to noop, while state persistence is a blind full replacement — so two same-key calls hydrate from one record and lose one set of changes (last write wins).

    On the linked PR. @larry-zy #2846 (OPEN, +1159/−84, 11 files) already implements exactly this shape — an in-process guard keyed by SandboxIsolationKey plus SandboxLifecycleMiddleware/HarnessAgent wiring — so no automated PR from us; a second generated patch would duplicate it. It will get reviewed on the scheduled PR rotation.

    Two review points worth carrying into #2846:

    1. Making a blocking guard the default changes behavior for users who intentionally run concurrent same-key calls, and tryEnter blocks a thread inside Reactor Mono.using — the default-on question should be an explicit, documented design decision.
    2. Complementary hardening the PR does not cover: the state slot could use the existing saveIfVersion CAS (AgentStateStore.java:87) to detect/reject lost updates even when a guard is bypassed (e.g. multi-instance without a distributed guard). Note that moving acquire/persist inside the delegate gate would not suffice, since USER/AGENT/GLOBAL keys are coarser than the gate's (userId, sessionId) key.

    Caveats. Static review only — the race was not reproduced at runtime; interleaving is inferred from eager Mono.using acquisition semantics and plain-save writes.


    Automated evaluation by github-manager-bot

  3. added
    area/harness
    on Sep 24, 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

    area/harnessbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions