-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
syscall: permit setting bInheritHandles to false when calling CreateProcess on Windows #36550
Comments
On Unix systems we mark all open files as close-on-exec, meaning that they are automatically closed when starting a new child process. Are we not doing that on Windows? Does Windows have any similar functionality? Certainly if at all possible we should be doing this in the standard library rather than requiring the program to use some interface to list the descriptors that should be closed. |
@ianlancetaylor In the implementation of os.startprocess, binherithandles of CreateProcess is always true. I hope it can be configured in sysprocatt. |
As far as I looked some code of os package in short time, os.Startprocess take ProcAttr in third argument. ProcAttr have Files field to pass handles to child process. Do you mean that you don't want that this files is duplicated with DuplicateHandle always? |
@mattn I can't get the fds, so I hope that the child process doesn't need to inherit the fds of the parent process,Win provides the binherithandles property on CreateProcess API. i not found linux api property. consider that all FDS should be closed before fork exec, except 0 1 2 fd. |
this is python subprocess.popen for linux: |
I believe normal Go program does not leave any parent process file handles opened in child process. Except 3 handles of child stdin, stdout and stderr.
Yes. CreateProcess bInheritHandles is always set to true by Go. But that is not enough to make a process handle inherited by a child. From
So only inheritable handles are inherited by child process. And Go does not have any inheritable handles. Except the 3 I described above. @zhaoya881010 do you actually have some problem to show us? Or you just read source code and think that the code is wrong? Alex |
@alex |
socket handles are inheritable by default |
@alexbrainman Thanks for the details. @zhaoya881010 Socket handles are inheritable by default, but sockets created by Go programs are always created with I'm going to close this because it sounds like things are working as expected. Please comment if you disagree. |
FYI, As far as I know, socket handles are process-specific-resources on Windows. So when the parent process exits, the socket is closed always. Also you can't pass the handle to another process without duplicating. To duplicate socket handle, you need to call WSADuplicateSocket. https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketa If WSADuplicateSocket is called, WSAPROTOCOL_INFOA can be extracted as byte data (ex file). Now the server process can close the socket and exit. Then child process can read the bytes from the file, and make socket handle from the WSAPROTOCOL_INFOA with WSASocketA. https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa Below is an example code to do it. https://github.com/mattn/gospel/blob/master/fd_windows.go#L13-L74 |
@ianlancetaylor @mattn @alexbrainman
Question:
|
It seems a bit sloppy for a library to allocate a socket but not provide any way for you to set Right now, If we can resolve that, I guess it would be OK with me if we added a field to |
From STARTUPINFOW doco
and
So, if CreateProcess bInheritHandles is false, then STARTF_USESTDHANDLES cannot be used, and lpStartupInfo.hStdInput cannot be used, so child process input will be keyboard. Same for stdout and stderr. But, we could, probably, use new STARTUPINFOEX to overcome this problem. https://devblogs.microsoft.com/oldnewthing/?p=8873 Alex |
This is a very common problem:https://bugs.python.org/issue19764 |
Possibly related: #24328 |
@odeke-em How is it going? |
Isn't this already fixed? SysProcAttrs has a field for it. |
@zx2c4 ok,i'll try. |
@zx2c4 @zhaoya881010 You can set this by:
Unfortunately it is broken as of 1.17: |
This has fixed in https://golang.org/cl/350416. Closing as there is nothing else to do here. |
How to set this function so that the child process does not inherit the parent process fds?
CreateProcess args:bInheritHandles on windows,subprocess.popen args close_fds by python on linux.
Why doesn't os.Startprocess provide interface.
In some cases, when the main process exits and the exec process is always there, the socket will not be released.
The text was updated successfully, but these errors were encountered: