fix(tests): pin pytest-asyncio to a session-scoped event loop - #175
Merged
Conversation
pytest-asyncio 1.x ignores the old session-scoped `event_loop` fixture and defaults both fixtures and tests to function-scoped loops (`asyncio_default_fixture_loop_scope=None`, `asyncio_default_test_loop_scope= function`). The integration suite shares a session-scoped engine whose in-memory SQLite (aiosqlite) connection is bound to the loop it was created on; with per-test function loops that connection is used from a different, already-closed loop. On Linux CI this surfaced as a cascade of `RuntimeError: Event loop is closed` / `sqlite3.OperationalError: no such table: users` across ~400 integration tests (201 failed + 202 errors), while macOS happened to tolerate it — so the suite passed locally but red-gated the Heroku auto-deploy, which only fires on a green CI run on main. - Set `asyncio_default_fixture_loop_scope = "session"` and `asyncio_default_test_loop_scope = "session"` so the shared engine and every test run on one session loop (restores the pre-1.x behaviour). - Drop the now-ignored custom `event_loop` fixture (and its now-unused `asyncio` / `pytest` imports). Verified: full backend suite 4494 passed locally, 0 "no such table", 0 "Event loop is closed". 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
backend-lint-test→ Integration tests was failing onmainwith 201 failed + 202 errors — a cascade ofRuntimeError: Event loop is closedandsqlite3.OperationalError: no such table: users. The full suite passed locally (macOS), so it only red-gated CI — and the Heroku auto-deploy (deploy.yml) only fires on a green CI run on main, so this blocked all deploys.Root cause
pytest-asyncio 1.x ignores the old session-scoped
event_loopfixture and defaults to function-scoped loops:The integration suite shares a session-scoped engine whose in-memory SQLite (aiosqlite) connection is bound to the loop it was created on. With per-test function loops, that connection is used from a different, already-closed loop →
Event loop is closed→ the in-memory DB is gone →no such tablefor every subsequent test. Linux surfaced it; macOS happened to tolerate it.Fix
asyncio_default_fixture_loop_scope = "session"+asyncio_default_test_loop_scope = "session"so the shared engine and every test run on one session loop (restores pre-1.x behaviour). Verified against the pytest-asyncio 1.x docs.event_loopfixture and its unused imports.Verification
0"no such table",0"Event loop is closed".ruff format --check+ruff checkclean.Stacked on #174 (StaticPool) and #173 (the original checkpoint-button fix).
🤖 Generated with Claude Code