Skip to content

fix(sandbox-agentrun): keep MCP open when sandbox is not owned - #2302

Open
jialiuyang wants to merge 1 commit into
agentscope-ai:mainfrom
jialiuyang:fix/agentrun-shutdown-keep-mcp-when-not-owned
Open

fix(sandbox-agentrun): keep MCP open when sandbox is not owned#2302
jialiuyang wants to merge 1 commit into
agentscope-ai:mainfrom
jialiuyang:fix/agentrun-shutdown-keep-mcp-when-not-owned

Conversation

@jialiuyang

Copy link
Copy Markdown

AgentScope-Java Version

2.0.x (main)

Description

AgentRunSandbox.shutdown() closed the MCP channel before checking isSandboxOwned(). For shared/non-owned sandboxes (cloud container kept alive), background SessionTree mirrors then failed with MCP session with server terminated while uploading session files.

This change returns early when the sandbox is not owned — keeping MCP open and skipping delete — matching E2bSandbox / DaytonaSandbox. Owned sandboxes still close MCP and delete as before.

Fixes #2259

Checklist

  • Code has been formatted with mvn spotless:apply
  • Targeted tests passing
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated — N/A
  • Code is ready for review

AgentRunSandbox.shutdown() closed the MCP channel before checking isSandboxOwned().
For shared/non-owned sandboxes that left the cloud container alive, background
SessionTree mirrors then failed with MCP session terminated. Skip close/delete when
not owned, matching E2b and Daytona adapters.

Fixes agentscope-ai#2259
@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/extensions agentscope-extensions (general) labels Jul 19, 2026

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR fixes the AgentRunSandbox.shutdown() lifecycle for non-owned (shared/reused) sandboxes. Previously, the MCP channel was unconditionally closed before the ownership check, meaning shared sandboxes lost their MCP connection even though background operations (e.g., SessionTree mirror uploads, ref #2259) might still need it. The fix moves the isSandboxOwned() guard before mcp.close(), aligning with the E2b/Daytona pattern where non-owned sandboxes trigger an immediate early return — neither closing the communication channel nor deleting the sandbox. The change is small, focused, and well-tested.

public void shutdown() throws Exception {
// Match E2b/Daytona: a non-owned sandbox is shared/reused — keep the MCP channel open so
// background SessionTree mirrors can still uploadFiles() after agent teardown (#2259).
if (!arState.isSandboxOwned()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Observability: The early return for non-owned sandboxes is silent. Consider adding a log.debug before returning so that lifecycle issues (e.g., unexpected double-shutdown or leaked MCP connections) are easier to trace in production.

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR fixes the AgentRunSandbox.shutdown() lifecycle for non-owned (shared/reused) sandboxes. Previously, the MCP channel was unconditionally closed before the ownership check, meaning shared sandboxes lost their MCP connection even though background operations (e.g., SessionTree mirror uploads, ref #2259) might still need it. The fix moves the isSandboxOwned() guard before mcp.close(), aligning with the E2b/Daytona pattern where non-owned sandboxes trigger an immediate early return — neither closing the communication channel nor deleting the sandbox. The change is small, focused, and well-tested.

public void shutdown() throws Exception {
// Match E2b/Daytona: a non-owned sandbox is shared/reused — keep the MCP channel open so
// background SessionTree mirrors can still uploadFiles() after agent teardown (#2259).
if (!arState.isSandboxOwned()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Observability: The early return for non-owned sandboxes is silent. Consider adding a log.debug before returning so that lifecycle issues (e.g., unexpected double-shutdown or leaked MCP connections) are easier to trace in production.

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR contains 101 lines of changes. Review in progress.


Automated review by github-manager-bot

@jialiuyang

jialiuyang commented Jul 24, 2026

Copy link
Copy Markdown
Author

Could a maintainer merge when convenient? Happy to rebase or adjust if anything else is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/extensions agentscope-extensions (general) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants