Documentation
¶
Overview ¶
Package procout starts a child process with its output read in a way that lets the process's exit be seen while a descendant still holds that output open.
exec.Cmd.StdoutPipe requires every read to finish before Wait is called, so a caller that reads to EOF first never sees the process exit while a descendant that inherited stdout or stderr is alive. Unreal Editor's detached UnrealTraceServer is one: it leaves the task's process group and outlives the editor. A service an OpenJD onEnter action starts in the background is another.
The child writes into OS pipes that procout itself reads, so exec's Wait returns at the exit. procout then waits for the output to close, or, once no process of the child's own group is left, for at most DrainDelay. After that the caller's readers end, but the pipes are still read and the output thrown away, so a process that holds them is never killed by SIGPIPE for writing.
It depends only on the standard library, so any sub-package of internal/worker can import it without creating a cycle.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DrainDelay = 5 * time.Second
DrainDelay is how long output is still delivered after the process has exited and no process of its group is left, for a descendant that still holds it open. Output written after that is not delivered. A variable so tests can shorten it.
Functions ¶
This section is empty.
Types ¶
type Output ¶
Output is a started process's stdout and stderr.
func Start ¶
Start starts cmd with its stdout and stderr connected to the returned Output's readers. The caller reads both, or calls Discard, and then calls Wait.
func (*Output) Abort ¶
Abort kills the process, drops its output and reaps it, for a caller that started the process but cannot go on with it. It returns the kill error; the process's exit status is of no interest after a forced kill.
func (*Output) Discard ¶
func (o *Output) Discard()
Discard reads and drops both streams, for a caller that will not read them. It returns at once.
func (*Output) Exited ¶
func (o *Output) Exited() <-chan struct{}
Exited returns a channel that is closed once Wait has seen the process exit, before Wait waits for the output to close. A caller that must not act on a process that has already exited, while its output is still being drained, selects on it. It is closed only by Wait, so it never closes for a caller that does not call Wait.
func (*Output) Wait ¶
Wait waits for the process to exit, then for its output to close, and returns the process's exit error. groupAlive reports whether any process of the child's own group (its process group, or its job object on Windows) is still running; while one is, Wait keeps waiting, as it would for a child it had not detached from. Once none is, or when groupAlive is nil, the output is delivered for at most DrainDelay more. stillOpen reports that it had not closed by then, which means a process outside the group holds it; the caller's readers then reach EOF after what was delivered.