Skip to content

[Bug]: Declared subagent spawned inside a sandboxed call self-deadlocks on SandboxExecutionGuard (USER/GLOBAL scope) #4440

Done

Description

@Ernest-L

Summary

When a sandboxed HarnessAgent spawns a declared subagent (subagents/*.md, default WorkspaceMode.ISOLATED) synchronously via agent_spawn, the subagent acquires its own sandbox and blocks forever on SandboxExecutionGuard.tryEnter(). The parent holds the guard for the whole call window, and the child - spawned inside that window - asks for the same key.

Why this is a bug

SandboxExecutionGuard's javadoc defines its purpose as serializing callers that could "race on the same persistent state slot (last write wins)". Parent and child do not share a state slot:

  • SessionSandboxStateStore#slotSessionId for USER scope is sandbox/user/{agentId}/{userId} - so parent and child use different slots (different agentId);
  • but SandboxIsolationKey.resolve(USER, ctx, agentId) returns only userId - so both map to the same guard key.

The guard therefore serializes two executions that the state layer treats as independent. Combined with the documented lifecycle ("the guard covers the full call window: acquire -> start -> (call) -> stop -> release"), a nested subagent can never proceed.

The default guard is noop(), so a plain HarnessAgent.builder().filesystem(spec) does not deadlock. It deadlocks with any serializing guard - including the recommended production setup distributedStore(...): RedisDistributedStore, JdbcDistributedStore (MySQL/PostgreSQL), MongoDistributedStore and the control-plane store each override sandboxExecutionGuard() with a real implementation.

To Reproduce

  1. Sandboxed agent with a distributed store (default IsolationScope.USER):
HarnessAgent agent = HarnessAgent.builder()
    .name("parent")
    .model(model)
    .workspace(workspace)
    .stateStore(redisAgentStateStore)
    .distributedStore(redisDistributedStore)   // injects RedisSandboxExecutionGuard
    .filesystem(new DockerFilesystemSpec()
        .image("ubuntu:24.04")
        .isolationScope(IsolationScope.USER))  // default
    .build();
  1. A declaration without workspace.mode (ISOLATED is the default):
<!-- workspace/subagents/worker.md -->
---
name: worker
description: does one focused task
---
You are a focused worker.
  1. Ask the model to agent_spawn(name="worker", ...) with timeout_seconds > 0 (synchronous, the default).

  2. The parent call hangs before the subagent runs. No exception is thrown.

With timeout_seconds = 0 (background) the child stalls until the parent call ends (the lease is still held) instead of deadlocking forever.

Expected behavior

At least one of:

  • the guard key should follow the state-slot granularity, so a nested subagent (different agentId) is not serialized against its ancestor;
  • or declared subagents should share the parent's sandbox in sandbox mode (opt-in builder flag), so no second acquisition happens;
  • or the combination should fail fast / be documented as unsupported.

Root cause (v2.0.5)

HarnessAgentBuilderSupport#buildDeclaredFactory gives an ISOLATED declared subagent the parent's sandbox spec (same code in main):

if (decl.getWorkspaceMode() == WorkspaceMode.SHARED && capturedSharedBackend != null) {
    sub.abstractFilesystem(capturedSharedBackend);
} else if (capturedSandboxSpec != null) {
    sub.filesystem(capturedSandboxSpec);   // same spec, but the child acquires its own sandbox
}

The child build() then creates its own SandboxBackedFilesystem, SandboxLifecycleMiddleware and SandboxManager, and acquires at call entry. SandboxManager.acquire resolves the key and blocks:

Optional<SandboxIsolationKey> scopeKey =
        SandboxIsolationKey.resolve(scope, runtimeContext, agentId);
lease = executionGuard.tryEnter(scopeKey.get());

Key vs slot granularity:

IsolationScope guard key persisted slot
USER (default) userId (agentId dropped) sandbox/user/{agentId}/{userId}
GLOBAL __global__ sandbox/global
SESSION sessionId sandbox/session/{sessionId}
AGENT agentId sandbox/agent/{agentId}

SESSION / AGENT scopes do not deadlock (parent and child keys differ), but they change isolation semantics - e.g. AGENT shares one slot across all users.

Environment

  • AgentScope-Java: 2.0.5 (also present in v2.1.0-BETA1 and main; SandboxManager, SandboxIsolationKey, SessionSandboxStateStore and SandboxExecutionGuard are byte-identical between 2.0.4 and 2.1.0-BETA1)
  • Java: 21
  • Backend: Docker sandbox (DockerFilesystemSpec) + distributed store with a real execution guard

Suggested fixes

  1. Derive a guard-specific key that includes agentId (leave the state-slot key unchanged). Sibling calls of the same agent still serialize; a nested subagent no longer collides.
  2. Or force declared subagents to SHARED when a sandbox spec is present, behind an opt-in builder flag.
  3. At minimum: build-time validation (sandbox + subagents + USER/GLOBAL + non-noop guard -> ERROR) and documentation of the SESSION workaround.

Related

Activity

  1. added
    bug
    on Nov 26, 2026
  2. Ernest-L commented on Nov 26, 2026

    @Ernest-L
    ContributorAuthor

    Implemented a fix in #4441. SandboxIsolationKey#forExecutionGuard(agentId) gives the execution guard the same granularity as the persisted slot (USER -> {agentId}/{userId}), so a declared subagent spawned inside a sandboxed call no longer blocks on the lease its parent holds for the whole call window. Persisted slot keys are unchanged (no migration); GLOBAL scope remains serialised by design.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions