Skip to content

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

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
jialiuyang:fix/agentrun-shutdown-keep-mcp-when-not-owned
Sep 21, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
jialiuyang:fix/agentrun-shutdown-keep-mcp-when-not-owned

Conversation

@jialiuyang

Copy link
Copy Markdown
Contributor

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
Collaborator

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
Contributor Author

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

@xzxiaoshan

Copy link
Copy Markdown
Collaborator

LGTM.

@jujn
jujn merged commit 8ca0080 into agentscope-ai:main Sep 21, 2026
5 checks passed
Nerv4rmore added a commit to Nerv4rmore/agentscope-java that referenced this pull request Sep 24, 2026
Conflict resolutions:
- ReActAgent.maybePatchPendingToolCalls: take upstream's shared
  MessageUtils.lastAssistantMessage(state.contextMutable()); fork's
  patchPendingToolCalls extraction was behaviour-equivalent and its
  findLastAssistantMsg() helper is deleted upstream.
- AgentRunSandbox.shutdown: keep fork side. Upstream agentscope-ai#2302 (keep MCP
  channel open for non-owned sandboxes) is already covered — the fork
  replaced the MCP channel with the HTTP data plane and shutdown() is a
  deliberate no-op, with the isSandboxOwned() guard in destroyInstance().
- WorkspaceContextMiddleware.buildWorkspaceParagraph: combine both —
  upstream agentscope-ai#3020 effectiveWorkspace plus fork's RuntimeContext for the
  sandbox working directory.
- Semantic fix: fork-only ToolRetryLater call site updated to the
  two-arg notifyPostActingHook(entry, false) introduced by agentscope-ai#2891.
- AgentRunSandboxShutdownTest (added by agentscope-ai#2302) rewritten against the
  HTTP data plane / destroyInstance semantics.
- Revision follows upstream at 2.0.4-SNAPSHOT.
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

5 participants