fix: use signal.NotifyContext for container cleanup on SIGINT/SIGTERM#2808
Open
fix: use signal.NotifyContext for container cleanup on SIGINT/SIGTERM#2808
Conversation
Replace manual goroutine-based signal handlers in predict and train commands with signal.NotifyContext. This ensures the context is cancelled on SIGINT/SIGTERM, causing in-flight operations to return and the deferred container cleanup to run reliably. Previously, the deferred predictor.Stop() would not execute when the process received a signal, leaving orphaned Docker containers running. Fixes #2799
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
cog predictandcog trainwithsignal.NotifyContext, ensuring containers are always cleaned up on SIGINT/SIGTERMFIXME: will not run on signalcomments since the issue is now resolvedProblem
Both
pkg/cli/predict.goandpkg/cli/train.gohad two competing signal-handling mechanisms, neither fully correct:signal.Notifythat caught SIGINT and calledpredictor.Stop(ctx)— but used the parent context which could already be cancelledpredictor.Stop(context.Background())— but Go defers don't run when the process exits from a signal handlerThis meant hitting Ctrl+C during
cog predictorcog traincould leave orphaned Docker containers running.Solution
Use
signal.NotifyContextto create a context that is automatically cancelled on SIGINT/SIGTERM. When the signal arrives, in-flight operations (e.g.predictor.Start(),predictor.Predict()) return with a context cancellation error, the function returns normally, and the deferredpredictor.Stop(context.Background())runs to clean up the container.Fixes #2799