[New-bugs-announce] [issue4216] subprocess.Popen hangs at communicate() when child exits

Amy report at bugs.python.org
Tue Oct 28 00:57:47 CET 2008

New submission from Amy <amy20_z at yahoo.com>:

I have a simple program to call a shell command "service cpboot start"
to start Check Point firewall on RHEL5.1.

#!/usr/bin/env python
# vim: softtabstop=4 shiftwidth=4 expandtab

import os
from subprocess import *

p = Popen('service cpboot stop',shell=True, stdout=PIPE)
output = p.communicate()
print 'STDERR: %s' % output[0]
print 'STDOUT: %s' % output[1]


Python process pid 13343 spawned child 13375 to run "service cpboot
start".  However, after child process 13375 finished and sent SIGCHLD to
the python script, the parent hangs in Popen function communicate() at
line 1041 and child process 13375 became a defunct process.

Traceback (most recent call last):
  File "./subprocess_test03.py", line 7, in ?
    output = p.communicate()
  File "/usr/lib/python2.4/subprocess.py", line 1041, in communicate
    rlist, wlist, xlist = select.select(read_set, write_set, [])

Here is part of the strace:

Process 13375 detached
[pid 19195] close(878)                  = -1 EBADF (Bad file descriptor)
[pid 19195] close(879)                  = -1 EBADF (Bad file descriptor)
[pid 19195] close(880)                  = -1 EBADF (Bad file descriptor)
[pid 13343] <... select resumed> )      = ? ERESTARTNOHAND (To be restarted)
[pid 19195] close(881 <unfinished ...>
[pid 13343] --- SIGCHLD (Child exited) @ 0 (0) ---
[pid 19195] <... close resumed> )       = -1 EBADF (Bad file descriptor)
[pid 13343] select(7, [4 6], [], [], NULL <unfinished ...>

It seems like the select system call got interrupted and error code was
"ERESTARTNOHAND" was returned. The PIPEs won't be able to terminate
since child process has finished and exited and EOF won't be read from
the PIPEs.

If executing the shell command directly from shell command line, there's
no problem at all.It seems like there might be some race condition
somewhere in the python library. 

Any idea what may cause the problem? Many thanks in advance.

components: Library (Lib)
files: subprocess_test03.py
messages: 75270
nosy: amy20_z
severity: normal
status: open
title: subprocess.Popen hangs at communicate() when child exits
type: crash
versions: Python 2.4
Added file: http://bugs.python.org/file11898/subprocess_test03.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list