test: bound serve_in_thread startup to prevent CI hangs#1897
Merged
Conversation
When `unused_tcp_port_factory()` returns a port that another xdist worker grabs before uvicorn can bind, the server thread exits without setting `server.started`, so the unbounded startup poll loop in `serve_in_thread` hangs until pytest-timeout kills the worker 30 minutes later. Bound the wait with a 30s deadline plus a `thread.is_alive()` check so the fixture fails fast with a clear error instead. Companion fix to 0c89b6e, which bounded the teardown.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1897 +/- ##
==========================================
- Coverage 92.86% 92.83% -0.03%
==========================================
Files 167 167
Lines 11699 11699
==========================================
- Hits 10864 10861 -3
- Misses 835 838 +3
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:
|
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.
Description
The session-scoped
http_serverfixture usesunused_tcp_port_factory()to pick a port, then starts uvicorn in a background thread. When another xdist worker grabs that port before this fixture'sbind()runs, the server thread fails with[Errno 98] address already in useand exits without ever settingserver.started = True. The startup poll loop inserve_in_threadhad 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:time.monotonic()based).thread.is_alive()check that detects when uvicorn has exited (bind failure being the realistic cause).Either condition raises a
RuntimeErrorwith 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