fix(fileutils): drain pipe buffers after process exits#124
Open
steps-re wants to merge 1 commit into
Open
Conversation
When run() breaks out of the read loop (p.poll() returns non-None), bytes that the subprocess wrote just before exiting may still be sitting in the kernel pipe buffer. With O_NONBLOCK a single read() call returns only what fits in one syscall, so a large burst of final output (common in nuclear simulation codes) could be silently dropped. Add a post-exit drain loop for both stdout and stderr. Once the write end of the pipe is closed (process exited), read_async() returns b'' (EOF) rather than raising EAGAIN, so the drain loop terminates cleanly. Add a regression test that emits 160 000 bytes of output—well past a typical 64 KB pipe buffer—and asserts every byte is captured. Closes watts-dev#49 Signed-off-by: Mike German <mike@stepsventures.com>
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.
Problem
fileutils.run()usesO_NONBLOCK+read_async()to avoid the classic pipe deadlock (write-end full while parent blocks reading the wrong fd). However, when the subprocess exitsrun()breaks out of the read loop as soon asp.poll()returns non-None— potentially before all data the process wrote has been drained from the kernel pipe buffer.With
O_NONBLOCKa singleread()syscall returns only what the kernel has ready at that instant; a large burst of output written just before process exit (common in nuclear simulation and optimization codes) can silently disappear. This is the underlying cause of the hang/truncation behaviour reported in #49.Fix
Add a post-exit drain loop for both
stdoutandstderr(2 + thewhile Trueloop infileutils.py). Once the write end of the pipe is closed,read_async()returnsb""(EOF) rather thanEAGAIN, so the loop terminates immediately after all buffered bytes are consumed.Test
Added
test_run_captures_all_outputwhich emits 160 000 bytes of output — well past a typical 64 KB pipe-buffer read — and asserts every byte is captured. The test isskipif win32(same as the rest of the non-blocking path).All 4
test_fileutilstests pass.Fixes #49
Signed-off-by: Mike German mike@stepsventures.com