test: fix flaky PTY env test#30
Merged
Merged
Conversation
test_PYTHONUNBUFFERED intermittently failed on CI with "'1' != None": the PYTHONUNBUFFERED line was missing from the captured env. Root cause is a race in how the test read the output. start() reads the process output on a separate thread that pushes chunks into a ReplayObservable and close()s it when done. Reading via read_until_closed() while that thread is still pushing races ReplayObservable.subscribe (replay buffered chunks, then register) against push (append chunk, fire to observers): a chunk pushed in the gap is neither replayed nor delivered, so the last env line could be dropped. The tiny, fast printenv.sh output made this most likely (and 3.14/loaded runners hit it). Fix the tests to wait for output_stream.wait_close() before reading: once closed there is no concurrent pusher, and close() keeps the replay buffer, so the late subscribe replays every chunk deterministically. Applied via a shared _run_and_read_all() helper to the env tests and the unicode/encoding tests (same latent race). No production code changed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stabilises
process_pty_test.py::TestEnvironmentVariables::test_PYTHONUNBUFFERED, which intermittently failed in CI (most recently on Python 3.14, PR #29) with:i.e. the
PYTHONUNBUFFEREDline was missing from the captured environment.Root cause — a read race against the output stream
start()reads the process output on a separate thread that pushes chunks into aReplayObservableandclose()s it when done. The tests read viaread_until_closed()while that thread was still pushing, after only athread.join(timeout=0.1).ReplayObservable.subscribe()(replay buffered chunks, then register the observer) is not synchronised againstpush()(append chunk, fire to observers). A chunk pushed in the window between "replay finished" and "observer registered" is neither replayed nor delivered live — so the last line of output could be dropped. The tiny, fastprintenv.shoutput made this most likely, and fast/loaded runners (3.14) hit it.Fix (test-only)
Wait for
output_stream.wait_close()before reading. Once the stream is closed there is no concurrent pusher, andclose()keeps the replay buffer (onlydispose()clears it, which these tests don't call), so the latesubscribereplays every chunk deterministically. Extracted as a shared_run_and_read_all()helper and applied to the env tests and the unicode/encoding tests (which had the same latent race).No production code changed.
Testing
process_pty_test.pyrun 20× locally: 20/20 pass (and the fix is deterministic by construction, not just lucky).Follow-up (out of scope)
The underlying
ReplayObservable.subscribe/pushrace is a real (if rare) concurrency gap that could also affect a production subscriber connecting mid-execution. Hardening it would mean adding synchronisation to that core class — deliberately left out of this test-only fix.🤖 Generated with Claude Code