Use sys.stderr.write() to fix stderr interleaving under make -jN#194
Merged
robherring merged 1 commit intoMay 7, 2026
Merged
Conversation
Member
|
Can we just fix the multi-line cases and leave the other print()'s alone. |
aditya-qti
commented
May 7, 2026
Author
I meant to write print("msg") here :( |
Member
|
Please squash the reverted parts so I can review it. |
single-line print() -> 1 write() syscall.
ex: print("msg") -> 1 write()
multi-line print() -> 2 write() syscalls.
ex: print(" line1 \n line2 \n line3 ") -> 2 write() syscalls - one for
"line1 \n line2 \n line3" and one for "\n" that print() adds at the end.
Python's sys.stderr is line-buffered by default, so the pending buffer
gets flushed when "\n" is seen.
p1 p2
write(" line1 \n line2 \n line3 ")
write("msg\n")
write("\n")
Output:
line1
line2
line3 msg
Fix the issue by using sys.stderr.write(text + "\n") instead of
print(text, file=sys.stderr), since sys.stderr.write(multi_line_text)
makes a single write() call.
Assisted-by: Claude:claude-opus-4-7
Signed-off-by: Aditya Chillara <achillar@qti.qualcomm.com>
a886edf to
ef09115
Compare
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.
Example interleaved stderr:
This issue was found when running
make -j$(nproc) dtbs_checkin PatchWise (automates patch review and static analysis):https://github.com/qualcomm/PatchWise/blob/main/patchwise/patch_review/static_analysis/dtbs_check.py#L36-L51
This makes it difficult for automations to find the diff between before and after errors of the current commit.
The root cause is that on Python's default line-buffered sys.stderr, print() makes 2 write() syscalls when
the input is a multi-line string, and a sibling process' write() can execute between those two syscalls.
single-line print() -> 1 write() syscall.
ex: print("msg\n") -> 1 write()
multi-line print() -> 2 write() syscalls.
ex: print(" line1 \n line2 \n line3 ") -> 2 write() syscalls
Python's sys.stderr is line-buffered by default, so the pending buffer gets flushed when "\n" is seen.
Output:
This can be proven by running strace on something like this:
Fix the issue by using sys.stderr.write(text + "\n") instead of print(text, file=sys.stderr),
since sys.stderr.write(multi_line_text) makes a single write() call.