Skip to content

Fix Stop not killing the command while it is starting - #119

Merged
daniel-nichter merged 1 commit into
go-cmd:masterfrom
yusuke-koyoshi:fix-stop-while-starting
Oct 23, 2026
Merged

daniel-nichter merged 1 commit into
go-cmd:masterfrom
yusuke-koyoshi:fix-stop-while-starting

Conversation

@yusuke-koyoshi

Copy link
Copy Markdown
Contributor

Problem

Since v1.4.3, if Stop is called after Start but before os/exec.Cmd.Start has returned, the command is never killed and runs to completion.

In this case Stop returns ErrNotStarted because c.started is still false. Before #105, Stop returned ErrNotStarted without changing any state, so the caller could wait and call Stop again, as discussed in #16 and implemented in #52. #105 moved c.stopped = true before that check, so calling Stop again now returns nil at if c.stopped without killing the command. run checks c.stopped only after each BeforeExec func, so without BeforeExec funcs nothing kills the command.

This happens when Stop is called shortly after Start, for example when a context is cancelled while the system is slow to start the process:

p := cmd.NewCmd("sleep", "5")
statusChan := p.Start()
err := p.Stop() // often ErrNotStarted
<-statusChan    // returns after 5s; calling Stop again returns nil

Fix

In run:

  1. Check c.stopped right before cmd.Start() and return early, even if there are no BeforeExec funcs. This is the same check that Fix issue 94: check if Stopped called after each BeforeExec func #105 does after each BeforeExec func.
  2. After cmd.Start() succeeds, check c.stopped under the same lock that sets c.started = true, and kill the command if Stop was called while it was starting.

With 2, if Stop takes the lock before c.started = true is set, run kills the command; otherwise Stop sees c.started and kills it as usual.

Stop still returns ErrNotStarted in these cases. The Stop doc comment no longer says that the third case is a race condition that callers must work around by waiting for Status.PID.

With 1, if Stop is called before Start, a later Start does not run the command, and the final Status has PID = 0, Exit = -1, and no start or stop time. In v1.4.3 this was already the case when there were BeforeExec funcs; now it is the same without them.

Tests

TestStopWhileStarting calls Stop right after Start and checks that the final Status is sent within 2 seconds with Complete = false and Exit = -1. On master it failed in 6 of 10 runs (timed out after 2 seconds); with this change it passed 50 of 50 runs with -race.

This test almost always goes through 1, so it does not cover the kill in 2. To check 2, I ran a separate test that calls Stop after a delay of 0 to 1.95 ms, 200 times. With the kill in 2 removed, the test timed out (the command was still running) by the third or fourth iteration in all three runs; with it, all 200 iterations passed in all three runs. I did not include that test because it depends on timing.

go test -race passes on macOS. I have not run the tests on Windows; GOOS=windows go vet passes, and on Windows run calls terminateProcess in the same way as Stop does.

If Stop was called after Start but before os/exec.Cmd.Start returned,
Stop flagged the command stopped and returned ErrNotStarted, but run
did not check the flag unless there were BeforeExec funcs, so the
command ran to completion. Calling Stop again returned nil without
killing the command because it was already flagged stopped. This is a
regression in v1.4.3 (go-cmd#105).

run now checks the flag right before starting the command, even if
there are no BeforeExec funcs, and kills the command right after
starting it if Stop was called in the meantime.
@daniel-nichter

Copy link
Copy Markdown
Member

Looks good. Thank you for the fix and test @yusuke-koyoshi!

@daniel-nichter
daniel-nichter merged commit 074d1de into go-cmd:master Oct 23, 2026
@daniel-nichter

Copy link
Copy Markdown
Member

Released in v1.4.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants