Skip to content

test: bound serve_in_thread startup to prevent CI hangs#1897

Merged
vdusek merged 1 commit into
masterfrom
test/bound-serve-in-thread-startup
May 15, 2026
Merged

test: bound serve_in_thread startup to prevent CI hangs#1897
vdusek merged 1 commit into
masterfrom
test/bound-serve-in-thread-startup

Conversation

@vdusek
Copy link
Copy Markdown
Collaborator

@vdusek vdusek commented 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

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.
@vdusek vdusek added t-tooling Issues with this label are in the ownership of the tooling team. adhoc Ad-hoc unplanned task added during the sprint. labels May 15, 2026
@vdusek vdusek self-assigned this May 15, 2026
@github-actions github-actions Bot added this to the 140th sprint - Tooling team milestone May 15, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.83%. Comparing base (4023314) to head (490a4a3).
⚠️ Report is 1 commits behind head on master.

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     
Flag Coverage Δ
unit 92.83% <ø> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek vdusek requested a review from Mantisus May 15, 2026 07:21
Copy link
Copy Markdown
Collaborator

@Mantisus Mantisus left a comment

Choose a reason for hiding this comment

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

LGTM

@vdusek vdusek merged commit 722c96b into master May 15, 2026
32 checks passed
@vdusek vdusek deleted the test/bound-serve-in-thread-startup branch May 15, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants