Skip to content

Close integration test async DB pool in fixture teardown - #14581

Merged
RayBB merged 1 commit into
internetarchive:masterfrom
pranshup04:14575/fix/integration-pool-teardown
Nov 19, 2026
Merged

RayBB merged 1 commit into
internetarchive:masterfrom
pranshup04:14575/fix/integration-pool-teardown

Conversation

@pranshup04

Copy link
Copy Markdown
Contributor

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-asyncio during runner teardown.


Technical

  1. Fixture Lifecycle Management (openlibrary/tests/core/conftest.py):

    • Converted the pg_db fixture from a synchronous @pytest.fixture to an @pytest_asyncio.fixture.
    • In pg_db's finally block, explicitly await async_db.close_pool() before clearing the registry and exiting the DatabaseJanitor context.
    • Root Cause: Previously, teardown only executed async_db._entries.clear(), which discarded the dictionary reference but never closed the AsyncConnectionPool. 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's Runner.close() / _cancel_all_tasks cancels it, but in psycopg-pool 3.3.3 the worker can return to await q.get(), causing the Python runner to wait indefinitely for the cancelled task. Awaiting close_pool() sends the workers their stop signals while the event loop is still running.
  2. Lifecycle Regression Test (openlibrary/tests/core/test_pg_db.py):

    • Added test_pg_db_closes_its_pool, which captures the pool during the test and asserts pool.closed is True immediately following fixture teardown.
  3. CI Observability & Diagnostics (.github/workflows/python_tests.yml):

    • Updated the integration test command to -vv -o faulthandler_timeout=60.
    • Retains the outer 300-second kill while ensuring that if any integration test ever hangs, pytest dumps thread tracebacks at 60 seconds with verbose test names instead of dying silently.

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):

  • Controlled Reproduction: With a diagnostic probe delaying the 2nd pool connection by 0.2s, the original fixture reliably reproduced the teardown hang after test_concurrent_adds_keep_one_row_and_return_saved_notes printed PASSED. Task dumps confirmed Runner.close() hung waiting on async-db-*-worker-* at q.get().
  • Regression Verification: test_pg_db_closes_its_pool failed against the original fixture (pool.closed remained False) and passed with this fix.
  • Forced-Race Validation: 10 consecutive runs of the forced-race probe passed cleanly with zero hangs.
  • Integration Suite Stress Test: 10 consecutive full runs of the core integration suite passed (730 total test executions).
  • Unit Suite: All 867 core unit tests passed.
  • Pre-commit: All 31 pre-commit hooks passed cleanly (ruff, mypy, zizmor, check-yaml, etc.).

Screenshot

Not applicable (test infrastructure and fixture lifecycle fix; no user-facing UI changes).


Stakeholders

@RayBB @cdrini

…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.
@openlibrary-bot
openlibrary-bot requested a review from a team November 18, 2026 20:49
@github-project-automation github-project-automation Bot moved this to Waiting Review/Merge from Staff in Ray's Project Nov 18, 2026
@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @pranshup04!

@RayBB is assigned to this PR and currently has:

  • 25 open PR(s) of equal or higher priority to review first.
PR triage checklist (maintainers / Pierre)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

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 openlibrary-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RayBB changed the title Close integration test async DB pool in fixture teardown (#14575) Close integration test async DB pool in fixture teardown Nov 19, 2026

@RayBB RayBB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@RayBB
RayBB merged commit b37e550 into internetarchive:master Nov 19, 2026
18 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting Review/Merge from Staff to Done in Ray's Project Nov 19, 2026
marley pushed a commit to marley/openlibrary that referenced this pull request Nov 19, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Flaky: booknotes integration tests sometimes hang until the CI timeout

3 participants