[PR #2043] [CLOSED] serve: fix potential race in NoLeakedSampler during test runs #3824

Closed
opened 2026-11-07 15:31:00 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2043
Author: @clothespin
Created: 10/12/2026
Status: ❌ Closed

Base: main ← Head: test-responses-sampler-race


📝 Commits (1)

  • a864f79 serve: fix potential race in NoLeakedSampler during test runs

📊 Changes

1 file changed (+7 additions, -8 deletions)

View changed files

📝 serve/test_responses.py (+7 -8)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2043 **Author:** [@clothespin](https://github.com/clothespin) **Created:** 10/12/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `test-responses-sampler-race` --- ### 📝 Commits (1) - [`a864f79`](https://github.com/Niko1221/Strata/commit/a864f790adc3c1712d565fcf7a25e61afd170415) serve: fix potential race in NoLeakedSampler during test runs ### 📊 Changes **1 file changed** (+7 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `serve/test_responses.py` (+7 -8) </details> ### 📄 Description ## 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:31:00 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3824
No description provided.