fix(tests): dispose the global app engine at session teardown - #176
Merged
Conversation
The green CI run still surfaced two non-fatal `PytestUnhandledThreadExceptionWarning: RuntimeError: Event loop is closed` annotations. They come from aiosqlite's `_connection_worker_thread` calling `call_soon_threadsafe` on the session event loop after it has closed: tests that exercise the real `app.models.base.async_session_factory` open connections on the module-level `engine` that nothing tears down, so with a session-scoped loop those connections' daemon threads outlive the loop. Add a session-scoped autouse fixture that disposes the global engine before the loop closes, eliminating the thread-exception noise. Verified: full backend suite 4494 passed locally (warnings 16 -> 15). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Problem
The (now green) CI run still emitted two non-fatal annotations:
Root cause
Tests that exercise the real
app.models.base.async_session_factoryopen aiosqlite connections on the module-levelengine, which nothing disposes. With the now session-scoped event loop, those connections' daemon worker threads outlive individual tests; when the session loop finally closes, the daemon callscall_soon_threadsafeon it →Event loop is closed, surfaced by pytest's thread-exception plugin. (The attribution to a specific test is approximate — thread exceptions are detected asynchronously;test_api_routesitself only uses mocked sessions.)Fix
Session-scoped autouse fixture in the shared
tests/conftest.pythat disposes the global engine before the loop closes.Verification
Full backend suite locally: 4494 passed (warnings 16 → 15),
ruffclean. CI confirms the annotations are gone.🤖 Generated with Claude Code