Skip to content

fix: use sys.stdout.write for thread-safe output in StdoutSink#140

Open
jaheyy wants to merge 1 commit intoawslabs:masterfrom
jaheyy:fix/stdout-thread-safety
Open

fix: use sys.stdout.write for thread-safe output in StdoutSink#140
jaheyy wants to merge 1 commit intoawslabs:masterfrom
jaheyy:fix/stdout-thread-safety

Conversation

@jaheyy
Copy link

@jaheyy jaheyy commented Mar 12, 2026

Summary

Fixes #139

Replace print(serialized_content) with sys.stdout.write(serialized_content + "\n" in StdoutSink to prevent interleaved output in multi-threaded environments.

sys.stdout.write(serialized_content + "\n") issues a single write call, which is atomic under CPython's GIL.

Verification

Tested with a repro script:

=== Running repro.py 10 times (output in /tmp/emf_repro.KLRzjP) ===

Run  1: 20000 lines, 0 interleaved
Run  2: 20000 lines, 0 interleaved
Run  3: 20000 lines, 0 interleaved
Run  4: 20000 lines, 0 interleaved
Run  5: 20000 lines, 0 interleaved
Run  6: 20000 lines, 0 interleaved
Run  7: 20000 lines, 0 interleaved
Run  8: 20000 lines, 0 interleaved
Run  9: 20000 lines, 0 interleaved
Run 10: 20000 lines, 0 interleaved

Total interleaved across 10 runs: 0

Replace print() with sys.stdout.write() to prevent interleaved output
in multi-threaded environments. print() makes two separate write calls
(content + newline) which can be interrupted between threads, while
sys.stdout.write() with an appended newline issues a single call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StdoutSink output is not thread-safe

3 participants