Repository navigation
Description
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
- Configure
AgentRunFilesystemSpecwith a non-owned sandbox (or a sandbox whereisSandboxOwned()returnsfalse) - Start a conversation that triggers agent execution and sandbox usage
- Observe the shutdown sequence after the agent responds
- 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) callsmcp.close()unconditionallyAgentRunMcpChannel.close()delegates toMcpSyncClientWrapper.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:
- Postpone
mcp.close()until all mirror operations complete (coordinate withSessionTree's executor) - Only close MCP when the sandbox is owned (since a non-owned sandbox remains alive and its MCP endpoint is still valid)
- Add a graceful shutdown mechanism to
SandboxBackedFilesystemthat waits for in-flight uploads
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
jialiuyang commentedon Jul 19, 2026
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.
fix(sandbox-agentrun): keep MCP open when sandbox is not owned
fix(sandbox-agentrun): keep MCP open when sandbox is not owned (agent…