贡献 / #2043

#2043 serve: fix potential race in NoLeakedSampler during test runs

open · @clothespin · 0 评论 · 去 GitHub 看

Server & APIWindowsLinux

说明

## Title
Issue: none of its own. The flake was noted as pre-existing in the validation notes of #1327, #1563, #1840 and #2023;
#1840 fixed it for its own new test file only.

## Summary
`NoLeakedSampler.test_server_close_ends_the_sampler` in `serve/test_responses.py` fails now and then with
`AssertionError: 1 != 2` (`serve/test_responses.py:284`). The server is not leaking anything; the test was counting
another test's thread. This changes the test only. `serve/` code is unchanged.

## What changed
`serve/test_responses.py` (+7 -8).

The test counted every thread in the process whose name ends in "(_loop)". `Telemetry.close()`
(`serve/telemetry.py:420`) only sets the stop flag, so the sampler of a server an earlier test closed can still be
taking its last reading when this test starts. The count before `serve()` is then 1, that thread ends while the new
server starts, and the count after is 1, not 2.

The test now notes the threads alive before `serve()` and takes the one new "(_loop)" thread as its own server's
sampler. After `server_close()` it waits up to 5 s for that thread to end, the same limit the old loop used.

Checked on Linux (Arch, Python from the repo's `.venv`):
- `python -m unittest serve.test_responses`, 30 runs each: 2 failures before (both this test, `1 != 2`), 0 after.
- With the failing order forced (an earlier server's sampler still alive when the test starts): fails 3 of 3 before,
  passes 3 of 3 after.
- With `Telemetry.close()` made a no-op, so a server really leaks its sampler: the test fails before and after, so it
  still catches the leak it was written for.
- Full discovery (`python -m unittest discover -s serve -t . -p "test_*.py"`): 706 tests, 8 skipped, 1 failure. That
  failure is `test_keepawake`'s `test_service_holds_for_the_whole_request_including_early_close`, which also fails on
  unchanged `main` and is the race #1901 fixes.

Not run: Windows, macOS.

本站相关内容

相关页面的快捷入口。