[New-bugs-announce] [issue12650] Subprocess leaks fd upon kill()
report at bugs.python.org
Thu Jul 28 15:59:00 CEST 2011
New submission from gabriele.trombetti <g.trombetti at plasmacore.com>:
There seems to be a file descriptor (fd) leak in subprocess upon call to kill() and then destroying the current subprocess object (e.g. by scope exit) if the pipe functionality is being used.
This is a reproducer:
(Linux 2.6.25, Python 2.7.1 (r271:86832))
import subprocess, time
subp = subprocess.Popen("echo hi; sleep 200; echo bye", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#you can do something here
#subp.wait() #Fixes the bug
#time.sleep(0.001) #fixes the bug
#time.sleep(0.0000001) #doesn't fix the bug
Launch the function multiple times interactively, each time the number of open file descriptors for the python process will increase by 2. You can see that by "ls -l /proc/<pid>/fd"
This seems to be a race condition because adding a time.sleep(0.001) before the return fixes the problem. Probably some kernel delay is responsible for the race.
This bug is significant for daemons because the daemon will die once the number of open file descriptors hits the ulimit, usually 1024, so please fix.
Note: until the bug is fixed, a simple workaround (not documented in module docs though) is to call Popen.wait() after Popen.kill()
components: Library (Lib)
title: Subprocess leaks fd upon kill()
versions: Python 2.7
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce