os/exec: Cmd.ProcessState field not always populated after (*Cmd.Run) returns #56002
Labels
Documentation
Issues describing a change to documentation.
FrozenDueToAge
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Read the documentation for the
os/exec
package'sCmd.ProcessState
field. Note that it promises that its value will be available "after a call toWait
orRun
.Write a program that calls on the
(*Cmd).Run
method that invokes a nominated command that does not exist (that is, theCmd.Path
field's value does not designate a readable, executable file), then attempt to call methods on the*os.ProcessState
field value that I expect to be present in theCmd
's "ProcessState" field.What did you expect to see?
The
Cmd
's "ProcessField" value would be a non-nil pointer to anos.ProcessState
.What did you see instead?
My program panics dereferencing a nil pointer. The
Cmd.ProcessState
field remains nil after(*Cmd).Run
returns.Diagnosis
(*Cmd).Run
is a macro operation that does the following:(*Cmd).Start
.(*Cmd).Wait
.It turns out that only after
(*Cmd).Wait
returns—assuming that(*Cmd).Start
was called first, and that it hasn't been called already—is theCmd.ProcessState
field populated. If(*Cmd).Run
's invocation of(*Cmd).Start
fails, then the field will not be populated.The documentation is simple, but misleading. It would be more accurate to say that the
Cmd.ProcessState
field will be populated after(*Cmd).Run
starts the command.I came upon this discrepancy while debugging a program that panicked in this situation. It took a while to figure out that the path to the command I was nominating in the
Cmd.Path
field was wrong, such that(*Cmd).Start
—invoked via(*Cmd).Run
—was failing. I figured that(*Cmd).Run
had returned, even if it had returned an error, so I could count on theCmd.ProcessState
field's value being available for use.The text was updated successfully, but these errors were encountered: