fix(cmd/docker): prevent race between force-exit goroutine and plugin wait#6878
Open
zampani-docker wants to merge 1 commit intodocker:masterfrom
Open
fix(cmd/docker): prevent race between force-exit goroutine and plugin wait#6878zampani-docker wants to merge 1 commit intodocker:masterfrom
zampani-docker wants to merge 1 commit intodocker:masterfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
… wait When a plugin ignores context cancellation and the user sends 3 SIGINTs, the CLI kills the plugin with SIGKILL. Previously the signal goroutine called os.Exit(1) directly; a race existed where plugincmd.Run() could return first (plugin was SIGKILL'd, so ws.ExitStatus() = -1) and the main goroutine would call os.Exit(-1) = exit code 255 before the goroutine reached os.Exit(1). Fix by moving exit-code ownership to the main goroutine. The signal goroutine closes forceExitCh before calling Kill(), guaranteeing the channel is closed before plugincmd.Run() returns (the plugin can only die after Kill() delivers SIGKILL; Run() only returns after the process is reaped). The main goroutine checks forceExitCh after Run() returns and performs the print + os.Exit(1) itself. Also return from the signal goroutine after the force-kill to prevent further loop iterations from calling close(forceExitCh) a second time (which would panic), in case additional signals arrive while the kill is in flight. Fixes a flaky failure in TestPluginSocketCommunication/detached/ the_main_CLI_exits_after_3_signals where exit code 255 was observed instead of 1 on loaded CI runners (RC Docker on Alpine). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Michael Zampani <michael.zampani@docker.com>
c52c101 to
2bc4307
Compare
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.
Summary
tryPluginRunwhere two goroutines could both callos.Exitwith different exit codes when force-killing a plugin after 3 SIGINT/SIGTERM signalsos.Exit(1)ownership to the main goroutine; the signal goroutine now only closes a channel and callsKill()returnafter the force-kill to prevent additional signals from causing acloseon an already-closed channel (panic)Root cause: when a plugin ignored context cancellation and 3 SIGINTs were sent, the signal goroutine called
plugincmd.Process.Kill()thenos.Exit(1). On a loaded runner,plugincmd.Run()in the main goroutine could return first — the SIGKILL'd plugin hasws.ExitStatus() = -1, leading toos.Exit(-1)= exit code 255 instead of 1.Fix:
forceExitChis closed beforeKill(). Since the plugin can only die after SIGKILL is delivered andRun()only returns after the process is reaped,forceExitChis guaranteed to be closed beforeRun()returns in the force-kill path. The main goroutine checks the channel and ownsos.Exit(1).Test plan
TestPluginSocketCommunication/detached/the_main_CLI_exits_after_3_signalscovers the fixed race — was flaky (exit code 255) on loaded CI runners with RC Docker on Alpine; should now be reliable🤖 Generated with Claude Code