os/exec: documentation unclear on whether (*Cmd).Wait
must be called
#52580
Labels
Milestone
(*Cmd).Wait
must be called
#52580
On POSIX platforms, a C program that uses
fork
to spawn a child process must subsequently callwait
or similar to reap any resulting zombies. However, the call towait
is not needed if the process explicitly sets the handler forSIGCHLD
toSIG_IGN
or sets theSA_NOCLDWAIT
flag on that handler.On those same platforms, Go's
os/exec
package usesfork
under the hood (viaos.StartProcess
,syscall.StartProcess
, and ultimatelysyscall.forkExec
). However,(*exec.Cmd).Start
doesn't document whetherWait
must actually be called. The documentation today says:That seems to imply that
Wait
must be called in order to release those resources, but that isn't entirely obvious to me — given the finalizers that the standard library already uses foros.File
, one might assume that the child process does not need to be explicitly reaped if the caller doesn't care about the exit code and hasn't allocated explicit resources that would need to be released (for example, because they setStdin
,Stdout
, and/orStderr
to eithernil
or instances of*os.File
).I think that
(*exec.Cmd).Start
should either clearly state thatWait
must always be called (and perhaps diagnose violations with a finalizer), or avoid unbounded resource leaks ifWait
is not called.I think the latter is fairly easy to implement at the cost of one extra goroutine per
Cmd
(by moving theWait
call into an eager goroutine), and that would also enable a clean fix for #28461 (#15103 notwithstanding).@ianlancetaylor, @bradfitz: does that seem like a reasonable resolution?
The text was updated successfully, but these errors were encountered: