Skip to content

os/exec: race between Wait and ctx.Done #16028

@ianlancetaylor

Description

@ianlancetaylor

There is an unlikely race condition when using os/exec.CommandContext. In Cmd.Wait, one goroutine sleeps in a select on ctx.Done and waitDone, while another goroutine sleeps in c.Process.Wait. If the process terminates at the same time as the context, then it is possible for the first goroutine to wake up and receive from ctx.Done and call c.Process.Kill just as the code in os.Process.Wait returns from syscall.Wait4. At that point the process ID is available for reuse, and a new process may be started with the same process ID. The call to c.Process.Kill will call c.Process.Signal. Because Process.wait has not yet had a chance to call Process.setDone, c.Process.Signal will call syscall.Kill with the old process ID. This could potentially kill an unrelated process.

This is similar to the problem described in #13987; the race described there has now been brought into the standard library.

Marking as 1.8 because I don't see a simple way to fix this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions