Skip to content
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

os/exec: when NoInheritHandles is set with a handle on Stdout or Stderr, executed command fails #45538

Closed
zhaoya881010 opened this issue Apr 13, 2021 · 8 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@zhaoya881010
Copy link

zhaoya881010 commented Apr 13, 2021

go version 1.16 windows/386

	out, err := os.OpenFile("test.out", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
	c := exec.Command("cmd", "/C", "dir")
	c.Stdout = os.Stdout // OK
	//c.Stdout = out // Error
	c.SysProcAttr = &syscall.SysProcAttr{NoInheritHandles: true}
	c.Start()
	c.Wait()

if use c.Stdout = out,c.Wait() return error exit status 1

@mknyszek mknyszek added this to the Backlog milestone Apr 13, 2021
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 13, 2021
@mknyszek mknyszek changed the title NoInheritHandles set True,exec.Command return error. os/exec: when NoInheritHandles is set, executed command fails Apr 13, 2021
@mknyszek
Copy link
Contributor

Is the problem here that NoInheritHandles is causing some sort of failure in the subprocess because the stdout handle isn't being propagated correctly?

I'm actually not sure what NoInheritHandles is supposed to guarantee here; I could see it implying that no handles are passed through at all -- you get a "bare" process in some sense that needs to set things up itself. But, I don't know enough about this to say.

CC @ianlancetaylor @bradfitz via https://dev.golang.org/owners

@mknyszek mknyszek added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 13, 2021
@mknyszek
Copy link
Contributor

I think it would be useful to get more information about what the behavior is that you expect in this scenario.

@mknyszek
Copy link
Contributor

Also, is this windows/386? win32 could technically also mean windows/arm.

@zhaoya881010
Copy link
Author

windows/386,i need NoInheritHandles set true and Redirection stdout,stderr to file.

@mknyszek mknyszek changed the title os/exec: when NoInheritHandles is set, executed command fails os/exec: when NoInheritHandles is set with a handle on Stdout or Stderr, executed command fails Apr 14, 2021
@networkimprov
Copy link

networkimprov commented May 15, 2021

cc @alexbrainman @mattn @zx2c4

@gopherbot remove WaitingForInfo

@gopherbot gopherbot removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label May 15, 2021
@zx2c4
Copy link
Contributor

zx2c4 commented May 15, 2021

This is intended behavior. Everything is working as expected. There is no bug here.

HOWEVER, if what you're looking for is that handles you don't specify aren't accidentally leaked to the child process, that will be the case in Go 1.17.

So: you can probably close this bug, and just wait til 1.17 to get the behavior you desire. I agree 1.16's semantics are worrisome, and understand why you would want this, which is why it's been changed. But for 1.16, that's how it is.

@zhaoya881010
Copy link
Author

@zx2c4 ok,expecting 1.17.

@qmuntal
Copy link
Contributor

qmuntal commented Jun 26, 2023

Can confirm this is working as expected, the child process can't write its output to stdout if it is not inherited.

@qmuntal qmuntal closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

7 participants