[issue41221] Output of print() might get truncated in unbuffered mode
Manuel Jacob
report at bugs.python.org
Tue Jul 7 02:14:11 EDT 2020
Manuel Jacob <me at manueljacob.de> added the comment:
It’s possible to trigger the problem on Unix with much smaller sizes, e.g. by interrupting the write() with a signal handler (even if the signal handler doesn’t do anything). The following script starts a subprocess doing a 16MiB write and sends a signal, which is handled but is a no-op, after reading a bit from the pipe:
import signal
import subprocess
import sys
CHILD_PROCESS = '''
import signal, sys
signal.signal(signal.SIGINT, lambda *x: None)
written = sys.stdout.write('x' * 16777216)
print('written:', written, file=sys.stderr, flush=True)
'''
proc = subprocess.Popen(
[sys.executable, '-u', '-c', CHILD_PROCESS],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
read = len(proc.stdout.read(1))
proc.send_signal(signal.SIGINT)
read += len(proc.stdout.read())
stdout, stderr = proc.communicate()
assert stdout == b''
print('stderr:', stderr)
assert read == 16777216, "read: {}".format(read)
% python3 test_interrupted_write.py
stderr: b'written: 16777216\n'
Traceback (most recent call last):
File "test_interrupted_write.py", line 24, in <module>
assert read == 16777216, "read: {}".format(read)
AssertionError: read: 69632
If I remove the '-u' that gets passed to the subprocess:
% python3 test_interrupted_write.py
stderr: b'written: 16777216\n'
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41221>
_______________________________________
More information about the Python-bugs-list
mailing list