agentscope-ai / agentscope-ai/agentscope-java
[Bug]: AgentRunSandbox.shutdown() closes MCP channel unconditionally, breaking background session file mirror
- 主要语言
- Java
- 星标
- 5.6k
- 派生
- 1.3k
- 平均合并
- 4 天 12 小时
- 30 天内合并 PR
- 77
描述
**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()`:
```java
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
贡献指南
评估
这个 Issue 还没有评估数据。