]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-138709: Fix race condition in test_external_inspection (#139209)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Sun, 21 Sep 2025 17:32:03 +0000 (18:32 +0100)
committerGitHub <noreply@github.com>
Sun, 21 Sep 2025 17:32:03 +0000 (18:32 +0100)
commit9df477c0ce7ac896d75d3bb06c3dd14808cd659a
tree735b838f9d88ed88361965f32e62efaa4cf0f0f7
parent080faf2d47f5b972557cbc660d898ae5d7d56ab7
gh-138709: Fix race condition in test_external_inspection (#139209)

Fix race condition in test_external_inspection thread status tests

The tests test_thread_status_detection and test_thread_status_gil_detection
had a race condition where the test could sample thread status between when
the sleeper thread sends its "ready" message and when it actually calls
time.sleep(). This caused intermittent test failures where the sleeper
thread would show as running (status=0) instead of idle (status=1 or 2).

The fix moves the thread status collection inside the retry loop and
specifically waits for the expected thread states before proceeding with
assertions. The retry loop now continues until:
- The sleeper thread shows as idle (status=1 for CPU mode, status=2 for GIL mode)
- The busy thread shows as running (status=0)
- Both thread IDs are found in the status collection

This ensures the test waits for threads to settle into their expected states
before making assertions, eliminating the race condition.
Lib/test/test_external_inspection.py