Repository navigation
Fix Stop not killing the command while it is starting - #119
Merged
daniel-nichter merged 1 commit intoOct 23, 2026
Merged
Conversation
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.
Member
|
Looks good. Thank you for the fix and test @yusuke-koyoshi! |
Member
|
Released in v1.4.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since v1.4.3, if
Stopis called afterStartbut beforeos/exec.Cmd.Starthas returned, the command is never killed and runs to completion.In this case
StopreturnsErrNotStartedbecausec.startedis still false. Before #105,StopreturnedErrNotStartedwithout changing any state, so the caller could wait and callStopagain, as discussed in #16 and implemented in #52. #105 movedc.stopped = truebefore that check, so callingStopagain now returns nil atif c.stoppedwithout killing the command.runchecksc.stoppedonly after eachBeforeExecfunc, so withoutBeforeExecfuncs nothing kills the command.This happens when
Stopis called shortly afterStart, for example when a context is cancelled while the system is slow to start the process:Fix
In
run:c.stoppedright beforecmd.Start()and return early, even if there are noBeforeExecfuncs. This is the same check that Fix issue 94: check if Stopped called after each BeforeExec func #105 does after eachBeforeExecfunc.cmd.Start()succeeds, checkc.stoppedunder the same lock that setsc.started = true, and kill the command ifStopwas called while it was starting.With 2, if
Stoptakes the lock beforec.started = trueis set,runkills the command; otherwiseStopseesc.startedand kills it as usual.Stopstill returnsErrNotStartedin these cases. TheStopdoc comment no longer says that the third case is a race condition that callers must work around by waiting forStatus.PID.With 1, if
Stopis called beforeStart, a laterStartdoes not run the command, and the final Status hasPID = 0,Exit = -1, and no start or stop time. In v1.4.3 this was already the case when there wereBeforeExecfuncs; now it is the same without them.Tests
TestStopWhileStartingcallsStopright afterStartand checks that the final Status is sent within 2 seconds withComplete = falseandExit = -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
Stopafter 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 -racepasses on macOS. I have not run the tests on Windows;GOOS=windows go vetpasses, and on WindowsruncallsterminateProcessin the same way asStopdoes.