test: bound serve_in_thread teardown to prevent CI hangs#1892
Merged
Conversation
The session-scoped `http_server` fixture's teardown called `thread.join()` without a timeout. When uvicorn occasionally ignored `should_exit`, the join blocked until pytest-timeout killed the run after 30 minutes. Bound the join, escalate to `force_exit`, and mark the worker thread as a daemon.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1892 +/- ##
==========================================
+ Coverage 92.82% 92.84% +0.01%
==========================================
Files 167 167
Lines 11699 11699
==========================================
+ Hits 10860 10862 +2
+ Misses 839 837 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
janbuchar
approved these changes
May 12, 2026
vdusek
added a commit
that referenced
this pull request
May 15, 2026
### Description The session-scoped `http_server` fixture uses `unused_tcp_port_factory()` to pick a port, then starts uvicorn in a background thread. When another xdist worker grabs that port before this fixture's `bind()` runs, the server thread fails with `[Errno 98] address already in use` and exits without ever setting `server.started = True`. The startup poll loop in `serve_in_thread` had no timeout, so it spun forever and pytest-timeout killed the whole worker 30 minutes later — marking every queued test on that worker as a timeout error. This is a companion to #1892, which bounded the **teardown**; the same class of bug lived in the **startup**. ### Fix In `serve_in_thread`, bound the wait with: 1. A 30s deadline (`time.monotonic()` based). 2. A `thread.is_alive()` check that detects when uvicorn has exited (bind failure being the realistic cause). Either condition raises a `RuntimeError` with a descriptive message, so the fixture fails fast and the failing test surfaces a clear error instead of a 30-minute hang. ### Observed failure https://github.com/apify/crawlee-python/actions/runs/25895087554/job/76106390899
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.
The session-scoped
http_serverfixture's teardown intests/unit/server.pycalledthread.join()with no timeout. When uvicorn occasionally fails to honorshould_exit(it's stuck inside the asyncio loop's selector poll), the join blocks indefinitely — pytest-timeout eventually kills the run after 30 minutes, taking down the whole job.The fix bounds the teardown:
thread.join(timeout=10)— wait for graceful shutdown.server.force_exit = Trueandthread.join(timeout=5)— uvicorn's documented hard-exit path.daemon=Trueso an abandoned thread cannot keep the interpreter alive at process exit.Observed failure: https://github.com/apify/crawlee-python/actions/runs/25720617454/job/75520659980