Repository navigation
Close integration test async DB pool in fixture teardown - #14581
Conversation
…hive#14575) - Convert pg_db to a pytest-asyncio fixture and await async_db.close_pool() before dropping the test database or closing the event loop. - Add test_pg_db_closes_its_pool lifecycle regression test. - Enable faulthandler_timeout=60 and verbose test logging in python_tests CI.
|
Thanks for the PR, @pranshup04! @RayBB is assigned to this PR and currently has:
PR triage checklist (maintainers / Pierre)
Note This comment was automatically generated by Pierre, Open Library's PR first-touch assistant, on behalf of @mekarpeles. This is NOT a code review or sign-off. |
openlibrary-bot
left a comment
There was a problem hiding this comment.
Fixes the flaky integration-test hang (#14575) by having the pg_db fixture actually close its async DB pool on teardown instead of just clearing the loop registry, and adds -vv/faulthandler_timeout to the CI command so a future hang prints a stack trace instead of a bare exit 124.
🎫 Ticket compliance analysis ✅
#14575 - PR Code Verified
Compliant requirements:
- Consider making the workflow print where a timed-out run was stuck
Requires further human verification:
- Find what the stalled test is waiting on and fix the cause
Dimensions
- Correctness — no findings. Checked pg_db async conversion, close_pool() teardown ordering, and all pg_db call sites; all are @pytest.mark.asyncio.
- Security — no published findings in this range. This is not a security audit.
- Accessibility — no findings. No template, JS, component or style changes in this range.
- Test coverage — no findings. New test_pg_db_closes_its_pool asserts pool.closed after teardown, a real regression test for the fix.
- Styleguide — no findings. Import order, fixture naming, and docstrings match surrounding conventions.
Tip
A new review starts automatically about 10 minutes after your last push. To start it sooner, a maintainer can re-request a review, or you can comment @openlibrary-bot review on this PR.
Note
Automated review by @openlibrary-bot. This is not an approval or a sign-off, and it does not satisfy a required review.
RayBB
left a comment
There was a problem hiding this comment.
Thanks @pranshup04, great root-causing! Clearing _entries without closing the pool was exactly the gap. I reproduced the hang on master by slowing the pool's second connection: booknotes stops after 5 tests like the CI log, then times out. With your branch it passes in a few seconds, and the full integration suite passes under the same slowdown. The pools_closed_after_test trick to assert after teardown is neat, and it fails against the old fixture. The faulthandler bit in CI is a nice touch too. LGTM!
…hive#14575) (internetarchive#14581) - Convert pg_db to a pytest-asyncio fixture and await async_db.close_pool() before dropping the test database or closing the event loop. - Add test_pg_db_closes_its_pool lifecycle regression test. - Enable faulthandler_timeout=60 and verbose test logging in python_tests CI.
Closes #14575
What does this PR achieve?
Closes each integration test's async PostgreSQL connection pool before its event loop shuts down, preventing unclosed background Psycopg pool workers from hanging
pytest-asyncioduring runner teardown.Technical
Fixture Lifecycle Management (
openlibrary/tests/core/conftest.py):pg_dbfixture from a synchronous@pytest.fixtureto an@pytest_asyncio.fixture.pg_db'sfinallyblock, explicitlyawait async_db.close_pool()before clearing the registry and exiting theDatabaseJanitorcontext.async_db._entries.clear(), which discarded the dictionary reference but never closed theAsyncConnectionPool. In concurrent tests (e.g.,test_concurrent_adds_keep_one_row_and_return_saved_notes), pool growth triggers a second connection in a background worker. If the test completes while that worker is active,pytest-asyncio'sRunner.close()/_cancel_all_taskscancels it, but inpsycopg-pool 3.3.3the worker can return toawait q.get(), causing the Python runner to wait indefinitely for the cancelled task. Awaitingclose_pool()sends the workers their stop signals while the event loop is still running.Lifecycle Regression Test (
openlibrary/tests/core/test_pg_db.py):test_pg_db_closes_its_pool, which captures the pool during the test and assertspool.closed is Trueimmediately following fixture teardown.CI Observability & Diagnostics (
.github/workflows/python_tests.yml):-vv -o faulthandler_timeout=60.Testing
Tested on Linux with Python 3.14.5 matching the CI runner's environment (Psycopg 3.3.5, psycopg-pool 3.3.3, pytest 9.2.0, pytest-asyncio 1.4.1.post1):
test_concurrent_adds_keep_one_row_and_return_saved_notesprintedPASSED. Task dumps confirmedRunner.close()hung waiting onasync-db-*-worker-*atq.get().test_pg_db_closes_its_poolfailed against the original fixture (pool.closedremainedFalse) and passed with this fix.ruff,mypy,zizmor,check-yaml, etc.).Screenshot
Not applicable (test infrastructure and fixture lifecycle fix; no user-facing UI changes).
Stakeholders
@RayBB @cdrini