Skip to content

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

Closed

Description

@Peng-io

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

Activity

  1. added
    bug
    area/core
    on Jul 17, 2026
  2. jialiuyang commented on Jul 19, 2026

    @jialiuyang
    Contributor

    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.

  3. added a commit that references this issue on Jul 19, 2026
    bd87249
  4. moved this from Backlog to In progress in AgentScope Javaon Jul 19, 2026
  5. moved this from In progress to Done in AgentScope Javaon Sep 21, 2026
  6. added a commit that references this issue on Sep 21, 2026
    8ca0080
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

    area/corebug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions