This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author gabriele.trombetti
Recipients gabriele.trombetti
Date 2011-07-28.13:58:59
SpamBayes Score 6.1215914e-08
Marked as misclassified No
Message-id <1311861540.35.0.103521770573.issue12650@psf.upfronthosting.co.za>
In-reply-to
Content
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
def leaktest():
    subp = subprocess.Popen("echo hi; sleep 200; echo bye", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #you can do something here
    subp.terminate()
    #subp.wait() #Fixes the bug
    #time.sleep(0.001) #fixes the bug
    #time.sleep(0.0000001) #doesn't fix the bug
    return True

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()

Thank you
History
Date User Action Args
2011-07-28 13:59:00gabriele.trombettisetrecipients: + gabriele.trombetti
2011-07-28 13:59:00gabriele.trombettisetmessageid: <1311861540.35.0.103521770573.issue12650@psf.upfronthosting.co.za>
2011-07-28 13:58:59gabriele.trombettilinkissue12650 messages
2011-07-28 13:58:59gabriele.trombetticreate