[issue3006] subprocess.Popen causes socket to remain open after close

Charles-Francois Natali report at bugs.python.org
Wed Jan 12 10:20:44 CET 2011


Charles-Francois Natali <neologix at free.fr> added the comment:

> I cannot figure out why the closesocket's graceful
shutdown is waiting for the Popen command to complete.

It doesn't. Your main process closes its socket. You could see it with a netstat/lsof (don't know under Windows).
The problem is that when you call subprocess.Popen(), there's a fork behind, and the child process receives a copy of the FD. That's why your socket is not deallocated until the subprocess completes (and closes the FD on exit).

> This problem does not show up with the equivalent os.popen command.

That's because os.popen() is implemented as subprocess.Popen with close_fds=True, so the socket is closed before execve is called. IMHO, setting close_fds=True by default is the Right Thing to do (and I think it's now done by default in py3k to avoid races between concurrent popen calls, in addition to setting pipe's FD as FD_CLOEXEC).

----------
nosy: +neologix

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3006>
_______________________________________


More information about the Python-bugs-list mailing list