agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: AgentRunSandbox.shutdown() closes MCP channel unconditionally, breaking background session file mirror

Open Peng-io opened this issue 4 months ago • 1 comments

Describe the bug

When using AgentRunFilesystemSpec with a non-owned sandbox (isSandboxOwned() == false), AgentRunSandbox.shutdown() unconditionally closes the MCP channel via mcp.close() on line 118, even though the cloud sandbox container is not deleted and remains alive. This severs the local MCP session while the background SessionTree.scheduleMirror() thread is still uploading session files via SandboxBackedFilesystem.uploadFiles(), causing the upload to fail with "MCP session with server terminated".

To Reproduce

  1. Configure AgentRunFilesystemSpec with a non-owned sandbox (or a sandbox where isSandboxOwned() returns false)
  2. Start a conversation that triggers agent execution and sandbox usage
  3. Observe the shutdown sequence after the agent responds
  4. Check logs for the following error from the mirror thread:
    WARN  [sandbox-fs] uploadFiles failed for path: agents/r-agent/sessions/1.jsonl
    java.lang.RuntimeException: MCP session with server terminated
    

Expected behavior

The MCP channel should not be closed while the background mirror thread still has pending uploads. Alternatively, if the sandbox is not owned (i.e. the cloud container is not being deleted), the MCP connection should remain open since the remote side is still alive and capable of receiving file writes.

Error messages

WARN  [sandbox-fs] uploadFiles failed for path: agents/r-agent/sessions/1.jsonl
java.lang.RuntimeException: MCP session with server terminated
    at io.modelcontextprotocol.spec.McpClientSession.lambda$dismissPendingResponses$1(McpClientSession.java:143)
    ...
Caused by: io.modelcontextprotocol.spec.McpTransportSessionClosedException: MCP session with ID ... has been closed

The root cause chain:

  • AgentRunSandbox.shutdown() (line 118) calls mcp.close() unconditionally
  • AgentRunMcpChannel.close() delegates to McpSyncClientWrapper.close(), terminating the MCP session
  • The SessionTree.scheduleMirror() background thread concurrently tries to upload session files
  • SandboxBackedFilesystem.uploadFiles() tries to exec a command via the now-closed MCP channel -> fails

Environment

  • AgentScope-Java Version: 2.0.1-SNAPSHOT (also reproduced on 2.0.0)
  • Java Version: 21.0.2
  • OS: Windows 11

Additional context

The relevant code in AgentRunSandbox.shutdown():

public void shutdown() throws Exception {
    try {
        mcp.close();                          // always closes MCP
    } catch (Exception ignore) {}

    if (!arState.isSandboxOwned()) {
        return;                                // cloud sandbox stays alive, but MCP is already dead
    }
    http.deleteSandbox(id);
}

Possible approaches for a fix:

  1. Postpone mcp.close() until all mirror operations complete (coordinate with SessionTree's executor)
  2. Only close MCP when the sandbox is owned (since a non-owned sandbox remains alive and its MCP endpoint is still valid)
  3. Add a graceful shutdown mechanism to SandboxBackedFilesystem that waits for in-flight uploads

Peng-io avatar Jul 16 '26 15:07 Peng-io

I'd like to work on this. Plan: align AgentRunSandbox.shutdown() with E2b/Daytona — when isSandboxOwned() is false, return without closing the MCP channel or deleting the sandbox, so background SessionTree mirrors can still upload. When owned, close MCP then delete as today.

jialiuyang avatar Jul 19 '26 08:07 jialiuyang