fix: clear leaked running loop in MCP client background thread#2111
Merged
mkmeral merged 1 commit intostrands-agents:mainfrom Apr 13, 2026
Merged
Conversation
When OpenTelemetry's ThreadingInstrumentor is active, Thread.run() is wrapped to propagate trace context, which leaks the parent thread's running event loop reference into child threads. This causes 'RuntimeError: Cannot run the event loop while another loop is running' when _background_task calls run_until_complete(). Add asyncio._set_running_loop(None) at the top of _background_task to clear any leaked loop state before creating the new event loop. This is the same pattern used by bedrock-agentcore-sdk-python (PR strands-agents#405).
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Assessment: Comment Well-scoped bug fix that addresses a real issue with OpenTelemetry's Review Details
Clean fix with good context in both the PR description and code comments. |
poshinchen
approved these changes
Apr 11, 2026
3 tasks
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.
Description
When
opentelemetry-instrumentation-threadingis active — the documented pattern for AgentCore observability — theThreadingInstrumentorwrapsThread.run()to propagate trace context. Combined withcontextvars.copy_context()inMCPClient.start(), this leaks the parent thread's running event loop reference into the background thread. When_background_taskthen callsrun_until_complete(), asyncio sees a "running" loop already set and raisesRuntimeError: Cannot run the event loop while another loop is running.This adds
asyncio._set_running_loop(None)at the top of_background_task()to clear leaked loop state before creating the new event loop. This is the same pattern the bedrock-agentcore team shipped in aws/bedrock-agentcore-sdk-python#405.Confirmed on SDK versions 1.28.0 and 1.35.0.
Related Issues
N/A
Documentation PR
N/A — no documentation changes needed.
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareAdded a unit test that simulates the OTEL leak by setting a running loop before
_background_taskexecutes, verifying the method clears it and completes successfully.Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.