Skip to content

proposal: os/exec, syscall: allow child processes to inherit all inheritable handles from the parent process #60942

Open
@qmuntal

Description

@qmuntal

On Windows, one must explicitly list the handles that will be inherited by a new process when calling syscall.StartProcess and os.StartProcess. This is problematic for applications that don't know which handles they have inherited from their parent process and want to spawn new child processes with the same inherited handles, as in #53652.

Windows does not provide an API to list all inherited handles, but suggest to use some form of process communication (source), so these applications are left behind without a solution other than reimplementing syscall.StartProcess.

In fact, before go1.17 we did support this use case, all inheritable handles were passed explicitly. #44011 changed syscall.StartProcess to use the more robust approach of passing them implicitly. What I'm proposing here is to allow users to opt into the old behavior by adding a new property to syscall.SysProcAttr property, named InheritAllInheritableHandles.

It would look like this:

type SysProcAttr struct {
        // ...
        // Existing props
	NoInheritHandles           bool                // if set, each inheritable handle in the calling process is not inherited by the new process
	AdditionalInheritedHandles []Handle            // a list of additional handles, already marked as inheritable, that will be inherited by the new process
	ParentProcess              Handle              // if non-zero, the new process regards the process given by this handle as its parent process, and AdditionalInheritedHandles, if set, should exist in this parent process
	// New props
	InheritAllInheritableHandles bool // if set and NoInheritHandles is unset, each inheritable handle in the calling process is inherited by the new process
}

If InheritAllInheritableHandles is set and NoInheritHandles is unset, then syscall.StartProcess would ignore AdditionalInheritedHandles and call the relevant windows APIs as before CL 288297. We could also fail in case both InheritAllInheritableHandles and NoInheritHandles are set, I'm still not sold on this.

Notice that handles passed in AdditionalInheritedHandles will still be inherited when InheritAllInheritableHandles is set, as they must be marked as inheritable to be usable in AdditionalInheritedHandles.

@golang/windows @alexbrainman @zx2c4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions