Add code to read from master_fd in the parent, breaking when we get an OSError

(EIO can occur on Linux) or there's no more data to read.  Without this,
test_pty.py can hang on the waitpid() because the child is blocking on the
stdout write.  This will definitely happen on Mac OS X and could potentially
happen on other platforms.  See the comment for details.
This commit is contained in:
Barry Warsaw 2007-04-13 16:12:02 +00:00
parent 9df5fa0d91
commit 9bd522d7d1
1 changed files with 18 additions and 0 deletions

View File

@ -115,6 +115,24 @@ if pid == pty.CHILD:
os._exit(4) os._exit(4)
else: else:
debug("Waiting for child (%d) to finish."%pid) debug("Waiting for child (%d) to finish."%pid)
# In verbose mode, we have to consume the debug output from the child or
# the child will block, causing this test to hang in the parent's
# waitpid() call. The child blocks after a platform-dependent amount of
# data is written to its fd. On Linux 2.6, it's 4000 bytes and the child
# won't block, but on OS X even the small writes in the child above will
# block it. Also on Linux, the read() will throw an OSError (input/output
# error) when it tries to read past the end of the buffer but the child's
# already exited, so catch and discard those exceptions. It's not worth
# checking for EIO.
while True:
try:
data = os.read(master_fd, 80)
except OSError:
break
if not data:
break
sys.stdout.write(data.replace('\r\n', '\n'))
##line = os.read(master_fd, 80) ##line = os.read(master_fd, 80)
##lines = line.replace('\r\n', '\n').split('\n') ##lines = line.replace('\r\n', '\n').split('\n')
##if False and lines != ['In child, calling os.setsid()', ##if False and lines != ['In child, calling os.setsid()',