[New-bugs-announce] [issue15898] OSX TTY bug

Andrew Moffat report at bugs.python.org
Mon Sep 10 02:58:14 CEST 2012

New submission from Andrew Moffat:

I'm getting some kind of race condition on OSX when trying to read the output from a pseudoterminal of a forked process.  This race conditional only occurs if I run tty.tcsetattr on the master side of the pty, regardless of if I actually change the mode or not.

Try running the following code on OSX multiple times.  Sometimes you'll see "testing", sometimes you won't, sometimes it hangs.  If you comment out "tty.tcsetattr", you will consistently see "testing".

import os
import pty
import resource
import signal
import tty

master, slave = pty.openpty()
pid = os.fork()

# we're not making any changes to the tty mode, but
# the mere act of setting a mode causes the output to only
# show up sometimes.
# comment out "tty.tcsetattr" and the bug goes away
mode = tty.tcgetattr(master)
tty.tcsetattr(master, tty.TCSANOW, mode)

# child process
if pid == 0:

    os.dup2(slave, 0)
    os.dup2(slave, 1)
    os.dup2(slave, 2)

    max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
    os.closerange(3, max_fd)

    # make controlling terminal.  taken from pty.fork
    tmp_fd = os.open(os.ttyname(1), os.O_RDWR)

    os.write(1, "testing".encode())


# parent process

        print(os.read(master, 1024))

        os.kill(pid, signal.SIGKILL)

messages: 170147
nosy: amoffat
priority: normal
severity: normal
status: open
title: OSX TTY bug
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list