Prevent Python subprocess from passing fds on Windows?

The Python subprocess module, by default, passes all open file descriptors to any child processes it spawns. This means that if the parent process is listening on a port and is killed, it cannot restart and start listening again (even using SO_REUSEADDR) because the child still has that handle. I have no control over the child process.

The subprocess POpen constructor takes a close_fds argument, which closes the handles for the child as I want. However, there is a Windows-only limitation that prevents it from being used if stdin / stdout is also overridden, which is what I need to do.

Does anyone know of this to work on Windows?

+1


source to share


2 answers


What seems to be the most relevant information I can find is: The SetHandleInformation that this article is linking to should give you pointers.



You will probably need to use pywin32 and / or ctypes to accomplish what you want.

+2


source


I do not have a window with a window so this is not tested, but I would be tempted to try the os.dup and os.dup2 methods; duplicate file descriptors and use them instead of parent ones.



-2


source







All Articles