Skip to content

fix(tests): dispose the global app engine at session teardown - #176

Merged
sshlg merged 1 commit into
mainfrom
fix/tests-dispose-app-engine
Jun 26, 2026
Merged

fix(tests): dispose the global app engine at session teardown#176
sshlg merged 1 commit into
mainfrom
fix/tests-dispose-app-engine

Conversation

@sshlg

@sshlg sshlg commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Problem

The (now green) CI run still emitted two non-fatal annotations:

PytestUnhandledThreadExceptionWarning
  File ".../aiosqlite/core.py", line 66, in _connection_worker_thread
    future.get_loop().call_soon_threadsafe(set_result, future, result)
RuntimeError: Event loop is closed

Root cause

Tests that exercise the real app.models.base.async_session_factory open aiosqlite connections on the module-level engine, 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 calls call_soon_threadsafe on 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_routes itself only uses mocked sessions.)

Fix

Session-scoped autouse fixture in the shared tests/conftest.py that disposes the global engine before the loop closes.

Verification

Full backend suite locally: 4494 passed (warnings 16 → 15), ruff clean. CI confirms the annotations are gone.

🤖 Generated with Claude Code

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>
@sshlg
sshlg merged commit c0845e6 into main Jun 26, 2026
@sshlg
sshlg deleted the fix/tests-dispose-app-engine branch June 26, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant