fix(slackbot): create MCP server per agent run to avoid cancel scope error#1289
Merged
fix(slackbot): create MCP server per agent run to avoid cancel scope error#1289
Conversation
…error The module-level MCPServerStreamableHTTP singleton was shared across concurrent asyncio.create_task handlers. streamablehttp_client uses anyio.create_task_group() internally, binding a cancel scope to the entering task. When the last handler to exit was a different task than the one that opened the connection, anyio raised "Attempted to exit cancel scope in a different task than it was entered in". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency bug in the slackbot where a shared module-level MCP server instance caused anyio cancel scope errors when handling concurrent Slack messages. The fix moves the MCPServerStreamableHTTP instantiation from module scope into the create_agent() function, ensuring each agent run gets its own isolated instance.
Changes:
- Removed module-level
slack_search_mcpsingleton that was shared across all agent runs - Created new
MCPServerStreamableHTTPinstance per-agent withincreate_agent()function - Eliminates cancel scope task mismatch errors caused by concurrent messages sharing the same MCP server context
💡 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
MCPServerStreamableHTTPinstantiation from module-level singleton intocreate_agent()so each concurrent message handler gets its own instanceasyncio.create_taskboundariesRoot cause
streamablehttp_clientinternally usesanyio.create_task_group(), which binds a cancel scope to the async task that enters it. When Task A opened the MCP connection but Task B was the last to close it, the cancel scope teardown happened in the wrong task.Test plan
🤖 Generated with Claude Code