fix: move MCP cleanup from Thread.__exit__ to orchestrator.run()#1261
Merged
Conversation
Thread.__exit__ is sync but MCP cleanup is async. Calling run_sync() from Thread.__exit__ while already in an async context (orchestrator.run()) caused issues with ContextVars and KeyboardInterrupt handling. The fix moves MCP cleanup to the orchestrator's finally block where it can be awaited directly. Cleanup only happens when the outermost Thread context exits (checked via get_current_thread() == None). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime error where MCP cleanup was attempted from a synchronous context (Thread.__exit__) while already in an async context, causing ContextVar corruption. The solution moves MCP server cleanup from Thread.__exit__ to the orchestrator's async finally block.
Key changes:
- Removed sync
_cleanup_mcp_servers()method fromThread.__exit__that was causing async/sync context conflicts - Added async cleanup logic to orchestrator's finally block with proper nested context handling
- Updated and added tests to verify cleanup happens in orchestrator, not Thread
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/marvin/thread.py |
Removed _cleanup_mcp_servers() method and cleanup logic from Thread.__exit__, simplifying it to only handle context reset |
src/marvin/engine/orchestrator.py |
Added MCP cleanup in orchestrator's finally block with check for outermost Thread context; imported cleanup_thread_mcp_servers and get_current_thread |
tests/agents/test_mcp_integration.py |
Updated test to verify cleanup happens in orchestrator not Thread; added regression test for async context cleanup |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the runtime error from #1260 where calling
run_sync()fromThread.__exit__while already in an async context caused issues.Problem
Thread.__exit__is sync but MCP cleanup is async. When the orchestrator (which is async) exits the Thread context, callingrun_sync(cleanup_thread_mcp_servers())would:run_sync_in_thread()ValueError: Token was created in a different ContextSolution
Move MCP cleanup from
Thread.__exit__(sync) toorchestrator.run()'s finally block (async), where it can be properly awaited:Test plan
test_mcp_cleanup_happens_in_orchestrator_not_threadto reflect new behaviortest_cleanup_from_async_context_worksregression test🤖 Generated with Claude Code