import os, subprocess, signal def setSigPipe(): "enable sigpipe exit in current process" signal.signal(signal.SIGPIPE, signal.SIG_DFL) if signal.getsignal(signal.SIGPIPE) != signal.SIG_DFL: raise Exception("SIGPIPE could not be set to SIG_DLF") # p1 should write until SIGPIPE is received p1 = subprocess.Popen(["yes"], stdout=subprocess.PIPE, preexec_fn=setSigPipe, close_fds=True) # p2 exits immediatly with and error p2 = subprocess.Popen(["false"], stdin=p1.stdout, close_fds=True) # this hangs because p1.stdout is still open in parent process, preventing # SIGPIPE from being generated. w1 = p1.wait() print "wait1",w1 w2 = p2.wait() print "wait2",w2