[Python-Dev] Avoiding file descriptors leakage in subprocess.Popen()

Facundo Batista facundobatista at gmail.com
Fri Jun 12 23:25:30 CEST 2009


In a long lived process at work, we started leaking file descriptors.

The problem were that subprocess.Popen._execute_child() method creates
two files descriptors through a call to os.pipe(), and after some work
it closes them. But an os.read() on the descriptor was interrupted
(EINTR), so an exception was raised, and the descriptors were not
closed... leakage!

This problem is easy to fix (I have a patch that fixes it, all tests
pass ok, see http://bugs.python.org/issue6274).

So, why this mail? I just think that the fix is ugly... it's not
elegant. It has the following structure:


errpipe_read, errpipe_write = os.pipe()
try:
    try:
        .....
        .....
        .....
        .....
        .....
        .....
    finally:
        os.close(errpipe_write)
    .....
    .....
    .....
finally:
    os.close(errpipe_read)


I just don't like a huge try/finally... but as FDs are just ints, do
you think is there a better way to handle it?

Thank you!!

Regards,

--
.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


More information about the Python-Dev mailing list