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

Open
zhaoya881010 opened this issue Apr 13, 2021 · 7 comments

Comments

@zhaoya881010
Copy link

@zhaoya881010 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 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

@mknyszek mknyszek commented Apr 13, 2021

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

Loading

@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented Apr 13, 2021

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

Loading

@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented Apr 13, 2021

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

Loading

@zhaoya881010
Copy link
Author

@zhaoya881010 zhaoya881010 commented Apr 14, 2021

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

Loading

@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 networkimprov commented May 15, 2021

cc @alexbrainman @mattn @zx2c4

@gopherbot remove WaitingForInfo

Loading

@zx2c4
Copy link
Contributor

@zx2c4 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.

Loading

@zhaoya881010
Copy link
Author

@zhaoya881010 zhaoya881010 commented May 20, 2021

@zx2c4 ok,expecting 1.17.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants