Fix event loop closed warning during stdio cleanup#2805
Draft
Conversation
Add GC call and yield after stdio_client context exits to clean up asyncio subprocess transport references while the loop is still running.
Add pytest filterwarnings to catch unawaited coroutine warnings as errors. Fix the underlying issue in _await_with_session_monitoring where a coroutine was not closed before raising when session task was already done.
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.
When using
Clientwith stdio transport in pytest, an "Event loop is closed" warning can appear during test teardown on Linux CI. This happens because asyncio'sBaseSubprocessTransport.__del__gets triggered by GC after pytest-asyncio closes the event loop.This PR attempts to fix the issue by forcing GC and yielding to the event loop after the
stdio_clientcontext manager exits but while the loop is still running. This should allow any subprocess transport cleanup callbacks to execute before the loop closes.Note: This fix may not fully resolve the issue - it needs to be tested in CI on Linux. The warning is fundamentally a race between GC and event loop closure, so there may still be edge cases. If this doesn't work, we may need to either:
Closes #2792
Tests added:
tests/client/test_stdio_cleanup.py- Tests that convertPytestUnraisableExceptionWarningto errors to catch the issue