Skip to content

Fix flaky unit test from aiosqlite failed-connect thread race - #250

Merged
Subhransu-De merged 1 commit into
mainfrom
fix/aiosqlite-failed-connect-race
Oct 11, 2026
Merged

Subhransu-De merged 1 commit into
mainfrom
fix/aiosqlite-failed-connect-race

Conversation

@Subhransu-De

Copy link
Copy Markdown
Owner

Summary

test (3.13) failed on main after #249 with PytestUnhandledThreadExceptionWarning: Exception in thread Thread-1 (_connection_worker_thread) / RuntimeError: Event loop is closed, reported against test_probes_over_http_when_the_database_is_down.

Cause

This is upstream bug omnilib/aiosqlite#394, which is still open in 0.22.1, the latest release.

  • A failed connect calls Connection.stop(), which queues the worker-thread shutdown with a future on the current loop and never awaits it.
  • test_readiness_fails_when_the_database_is_unreachable connects to an impossible SQLite path.
  • If that test's event loop closes before the worker thread reaches the shutdown item, the thread's call_soon_threadsafe raises. pytest then attributes the exception to whichever test runs next.

It is a timing race. It did not reproduce locally with CI's seed (3245780680), and it has not appeared in earlier CI runs. #249 does not touch the database layer.

Fix

The unreachable_settings fixture is now async. In teardown it joins any aiosqlite worker threads via anyio.to_thread.run_sync, so the event loop stays alive and keeps handling the thread's final callback until the thread exits.

Verification

  • make check passes.
  • The unit suite passes repeatedly with random ordering.

Opened by claude-opus-5-5

A failed aiosqlite connect queues its worker-thread shutdown without
waiting for it (omnilib/aiosqlite#394). When the test's event loop closed
first, the thread raised "Event loop is closed", and pytest reported it
as an unhandled thread exception in the next test. This flaked test (3.13)
on main after #249.

Join the worker threads in the unreachable_settings fixture teardown,
off the event loop, so the loop is still alive when the thread finishes.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T06:32:37.199220Z 132b15b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@sonarqubecloud

Copy link
Copy Markdown

@Subhransu-De
Subhransu-De merged commit 1260558 into main Oct 11, 2026
37 checks passed
@Subhransu-De
Subhransu-De deleted the fix/aiosqlite-failed-connect-race branch October 11, 2026 06:35
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