Pull requests / #3614

#3614 [Other] Test: NoLeakedSampler loses a still-ending sampler between its two name counts (flaky 1 != 2)

open · @victorgabr · 0 comentários · No GitHub

Server & APIWindowsLinux

Descrição

## Problem and result

`serve/test_responses.py` `NoLeakedSampler.test_server_close_ends_the_sampler` fails at random - once in 929 tests on CI (run 39625083194, step "Run full server suite without terminal packages") and about once in four plain `python -m unittest serve.test_responses` runs on Linux, always `AssertionError: 1 != 2`. After this change the module passes 8/8 on this machine.

## Cause

The test counts threads named `*(_loop)` before and after `serve()` and expects `before + 1`. The name is not set on purpose: Python 3.10+ names an unnamed `Thread` after its target, and the telemetry sampler (`serve/telemetry.py:584`, `target=self._loop`) shows up as `Thread-N (_loop)`. `Telemetry.close()` only sets `_stop`; the thread sits in `_stop.wait(1.0)` and ends up to a second after the close. A server closed by a test shortly before can still leave such a straggler: `before` counts it, it exits between the two counts, and the one thread this server started is hidden - `1 != 2`. The name-total check cannot tell the two threads apart.

## Approach

The test's own question - "one server, one sampler, and it ends with the server" - is answered by identity, not totals: record the set of sampler thread ids before, take the difference after `serve()` (a straggler leaving in between cannot affect the difference), and wait for that thread to end. Product behavior is unchanged: no `close()` or join changes, no sleeps added to the suite.

## Validation

**Passed** (Linux, Python 3.10.9):
- Before the fix: reproduced `AssertionError: 1 != 2` locally (1 failure in 4 module runs).
- After: 8/8 `python -m unittest serve.test_responses` OK, plus one more on the rebased branch.

**Failed**: none.

**Not run**: Windows and the 929-test suite locally (needs the engine build); the CI run will cover it.

## Limits

The underlying one-second exit latency of the sampler is kept - it is the sample cadence, not a bug; only the test's counting is fixed.

No site

Links install, modelos, releases.