Skip to content

test(client): make the constructor read-timeout case deterministic - #740

Merged
MateoLostanlen merged 2 commits into
pyronear:mainfrom
Adelagric:fix/client-read-timeout-flake
Nov 18, 2026
Merged

MateoLostanlen merged 2 commits into
pyronear:mainfrom
Adelagric:fix/client-read-timeout-flake

Conversation

@Adelagric

Copy link
Copy Markdown
Contributor

test_client_constructor expected Client(..., timeout=0.00001) to raise ReadTimeout against the real backend. It sometimes fails with DID NOT RAISE ReadTimeout, as in this run on #734, which does not touch client/.

Cause. A socket cannot wait less than about 1 ms: settimeout(1e-5) blocked for 1.0 to 1.15 ms on a silent peer. A full GET to the local backend takes 1.2 to 1.8 ms, so the case is a race and its outcome depends on the machine. Run on its own in separate processes, the old test failed 19 times out of 38 on my laptop. In CI, pytest-client failed on 1 of the last 40 runs of tests.yml.

Fix. The case moves to its own test, against a local socket that listens and never calls accept(). The kernel completes the handshake, so the client connects, sends its request and waits. The request, the timeout and the exception stay real, and no server thread is needed.

  • The timeout is 0.1 s. It also covers the connect phase, and a ConnectTimeout is not a ReadTimeout, so it needs some margin.
  • A timer closes the socket after 10 s. There is no pytest-timeout here, so a client that loses its timeout would otherwise hang the job.
  • client/pyroclient is unchanged. The other three rows of test_client_constructor still use the real backend.

Why not mock requests.get. A mock that raises ReadTimeout only checks that the constructor lets it through. It stays green when the constructor stops passing its timeout.

Known limit. The first GET times out, so the new test never reaches the token check. A constructor that swallowed the timeout on that second GET alone would still pass. The old case only got there by chance.

Test plan

Each change below was made on a copy of pyroclient, on macOS and in a Linux container:

Constructor change New test
Swallows Timeout fails, DID NOT RAISE ReadTimeout
Converts Timeout to ValueError fails
/status GET loses its timeout fails after 10 s, Connection reset by peer
Swallows Timeout on /status only passes, the next GET still raises
Swallows Timeout on the token check only passes, the known limit above

The new test had no failure in 4800 runs:

  • macOS: 1000 idle, 1000 with 42 busy loops;
  • Linux: 1000 idle, 1000 with 12 busy loops, 500 limited to 0.25 CPU, 300 limited to 0.25 CPU with 6 busy loops.

Against the docker-compose.dev.yml stack on macOS, the full client suite passes (19 passed). ruff check and ruff format --check are clean.

I ran these in a Python 3.11 venv with the locked requests, urllib3 and pytest versions, not through uv run. The CI of this PR will be the first run on a GitHub runner.

A socket cannot wait less than about 1 ms, and a local backend answers in
about as long, so the 0.00001 s case of test_client_constructor raced against
the server and sometimes failed with DID NOT RAISE ReadTimeout.

Test it against a local socket that accepts the connection and never responds.
The request, the timeout and the exception stay real, and the outcome no longer
depends on the speed of the machine. A timer closes the socket after 10 s, so a
client that loses its timeout fails the test instead of hanging the job.
@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.33%. Comparing base (4939d89) to head (96c434d).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #740      +/-   ##
==========================================
+ Coverage   95.12%   95.33%   +0.20%     
==========================================
  Files          60       61       +1     
  Lines        3364     3428      +64     
==========================================
+ Hits         3200     3268      +68     
+ Misses        164      160       -4     
Flag Coverage Δ
backend 95.48% <ø> (+0.20%) ⬆️
client 91.83% <ø> (+0.46%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 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.

MateoLostanlen
MateoLostanlen previously approved these changes Nov 7, 2026

@MateoLostanlen MateoLostanlen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @Adelagric, thanks for the PR!

Great root-cause analysis, and the silent socket is a clean way to make this deterministic without mocking.

  • (nit) All remaining rows of test_client_constructor now use timeout=10, so the timeout parameter can go.
  • (nit) The silent_host docstring says it "accepts connections", but nothing calls accept(). Maybe "queues connections"?

Happy to merge as is, or after these small tweaks if you want to make them.

…test

Every remaining row of test_client_constructor uses the same timeout, so it
no longer needs to be a parameter. The silent_host docstring now says the
socket queues connections, since nothing calls accept().
@Adelagric

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Both tweaks are in 96c434d: the timeout parameter is gone and the docstring now says "queues connections". CI is green.

The push dismissed your approval, sorry about that. Could you approve it again when you have a moment?

@MateoLostanlen MateoLostanlen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @Adelagric, thanks for the PR!

Both tweaks look good, approving again. Merging now.

@MateoLostanlen
MateoLostanlen merged commit 5d1c060 into pyronear:main Nov 18, 2026
22 checks passed
@Adelagric
Adelagric deleted the fix/client-read-timeout-flake branch November 18, 2026 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants