Repository navigation
Description
Describe the bug
SandboxBackedFilesystem and SandboxLifecycleMiddleware both assume that a given agent instance runs at most one call at a time, and store per-call sandbox state in agent-level single slots:
-
SandboxBackedFilesystemis a stable proxy created at agent build time and shared by all tool executions of the agent. The live sandbox is injected into a single field:// agentscope-harness/.../filesystem/sandbox/SandboxBackedFilesystem.java private volatile Sandbox sandbox; // injected per call, cleared on release
-
SandboxLifecycleMiddlewarekeeps the acquire result of "the current call" in a single reference:// agentscope-harness/.../middleware/SandboxLifecycleMiddleware.java private final AtomicReference<SandboxAcquireResult> currentAcquireResult = new AtomicReference<>();
When the same agent bean is called concurrently for different sessions (a common setup: one shared ReActAgent/HarnessAgent bean serving many users/conversations — the SDK's serializeOnKey only serializes calls of the same (userId, sessionId), cross-session calls run in parallel by design), the two slots are corrupted in three distinct ways:
-
Sandbox cross-talk — call B's
filesystemProxy.setSandbox(sbB)overwrites call A'ssbAwhile A is still running. All subsequent filesystem/tool operations of A silently land in B's sandbox: files leak across sessions, workspace projection of one conversation is visible to another. -
Spurious
No active sandboxfailures — when A finishes first,releaseForCallexecutesfilesystemProxy.setSandbox(null). B is still mid-run; its next filesystem operation throws:io.agentscope.harness.agent.sandbox.SandboxException$SandboxConfigurationException: No active sandbox — sandbox filesystem used outside of a call context -
Wrong-pair release + sandbox session leak —
releaseForCalldoescurrentAcquireResult.getAndSet(null). If B's acquire has overwritten the slot, A's release willpersistState/releaseB'sSandboxAcquireResult(stopping the sandbox B is actively using), while A's own acquire result is lost and its sandbox session is never released.
We hit (2) in production at scale: an agent that dispatches IM (DingTalk) group messages across many conversations had a large fraction of runs fail with No active sandbox whenever two conversations were active at once.
To Reproduce
- Build one
ReActAgent(harness) with a sandbox-backed filesystem and any filesystem tool (e.g.execute). - Fire two concurrent
call()s with differentsessionIds (soserializeOnKeydoes not serialize them), each running a tool loop that touches the filesystem for a few seconds. - Observe:
SandboxConfigurationException: No active sandboxon the longer-running call once the shorter one completes; with logging on the sandbox side, also observe call A's commands executing inside call B's sandbox session before that.
Expected behavior
Each in-flight call operates on its own sandbox for its entire duration, regardless of concurrent calls on the same agent instance. Acquire/release must be paired per call: a finishing call must never clear or release another call's sandbox.
Error messages
io.agentscope.harness.agent.sandbox.SandboxException$SandboxConfigurationException:
No active sandbox — sandbox filesystem used outside of a call context
at ...SandboxBackedFilesystem.requireSandbox(SandboxBackedFilesystem.java)
at ...SandboxBackedFilesystem.execute(SandboxBackedFilesystem.java)
Proposed fix: bind the sandbox to the per-call RuntimeContext instead of agent-level slots
The preconditions are already in place:
- Every
SandboxBackedFilesystemoperation (execute/uploadFiles/downloadFiles) already receives the per-callRuntimeContextas a parameter. RuntimeContextalready provides a typed attribute container (ctx.get(Class<T>)/ctx.put(Class<T>, T)).
Concrete changes:
-
Inject side — in
SandboxLifecycleMiddleware.acquireForCall(ctx), stop writing agent-level slots; store per call instead:ctx.put(Sandbox.class, sandbox); ctx.put(SandboxAcquireResult.class, result);
-
Use side — change
SandboxBackedFilesystem.requireSandbox()torequireSandbox(RuntimeContext ctx)resolving viactx.get(Sandbox.class); delete thevolatile Sandbox sandboxfield. Deprecate (or re-shape) theSandboxAwaresetter/getter accordingly. -
Release side —
releaseForCall(ctx)retrieves its ownSandboxAcquireResultfromctxforpersistState/release/lease.close(); deletecurrentAcquireResult.
This removes all three failure modes at once: no overwrite (each call holds its own reference), no spurious null-out (release only clears its own context entry), no wrong-pair release (acquire/release are naturally paired through the ctx object). Thread-safety across Reactor/Netty thread hops comes for free because the reference travels with the per-call RuntimeContext.
Follow-ups to consider in the same change:
- Audit call paths into the sandbox filesystem that pass a
null/sharedRuntimeContext— any such path is a latent bug and should be fixed alongside. SandboxAware#setSandbox/getSandboxis public API; keep a deprecated no-op for one release if needed.- Keep the
SandboxExecutionGuardextension point — it remains useful for genuine concurrency quotas (e.g. max sandboxes per user), it just should no longer be needed as a "serialize everything" workaround.
Alternatives considered
- Keyed map inside the proxy (callId → Sandbox): the use side has no reliable callId without ThreadLocal, and the execution hops across Reactor/Netty threads where ThreadLocal propagation breaks — fragile.
- Per-call filesystem instances: tools are bound to the filesystem proxy at agent build time; making that per-call would ripple through tool registration — far more invasive than the
RuntimeContextbinding.
Current workaround (for other users hitting this)
Implement a SandboxExecutionGuard with a fair single-permit Semaphore that serializes all calls of the agent (tryEnter → acquire, lease close → release). Note: use a Semaphore, not ReentrantLock — the lease is closed by the SDK during Mono.using cleanup on a Reactor/Netty thread different from the acquiring thread, and a cross-thread unlock() throws IllegalMonitorStateException, permanently leaking the lock. Cost of the workaround: cross-session calls degrade from parallel to FIFO queueing on that agent.
Environment (please complete the following information):
- AgentScope-Java Version: 2.0.0 (agentscope-core / agentscope-harness)
- Java Version: 17+
- OS: linux (production), macos (dev) — OS-independent
Additional context
Related (same concurrency assumption, previously reported separately): the session slot key userId + "/" + sessionId is parsed back via lastIndexOf('/'), which mis-splits when sessionId itself contains / (e.g. base64 DingTalk openConversationId).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
fix(harness): isolate sandbox binding per call to fix concurrent cor…
Merge upstream/main into fork dev-port-skill: sync 46 upstream commits