-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Cobra's command.Context()
, cancel execution when CLI is signalled
#2184
Conversation
Needs a rebase as we already bumped to docker/cli v25.0.0-rc.1 in #2175 |
ba90323
to
e7b264f
Compare
util/cobrautil/cobrautil.go
Outdated
|
||
signalLimit := 3 | ||
s := make(chan os.Signal, signalLimit) | ||
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we also need to handle termination signal for Windows (os.Interrupt
)
SGTM, but I think we may need eyes from @jsternberg and @tonistiigi, because I noticed the OTEL tracing packages appears to be (ab)using appcontext to set up tracing; buildx/vendor/github.com/moby/buildkit/util/tracing/env/traceenv.go Lines 16 to 18 in 78adfc8
Which is "dot-imported" in Line 8 in 78adfc8
|
Yes I was also going to report this one, we might need to handle context inits in |
Ohhhhh I would have totally missed that, thank you. Those "dot-imports" and |
I arrived there, because I was curious why the |
Make `detect.InitContext` public as opposed to only available through the use of contexts from `appcontext` so that downstream users (e.g. buildx) can keep the OTEL context utils without having to use `appcontext` - see: docker/buildx#2184 Signed-off-by: Laura Brehm <[email protected]>
Submitted moby/buildkit#4548 so that we can use |
e7b264f
to
8646014
Compare
I amended the PR with a broken commit changing the vendored code (to be the same as with moby/buildkit#4548) to showcase what this would look like after it gets merged. |
8646014
to
b2f7e80
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the desire is to always control the cobra context then why not simplify this to single
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
cmd.SetContext(appcontext.Context())
}
I think this has the same behavior as this PR without wrapping individual commands.
Regarding (future?) handling of the termination signal via unix socket close, this can also be done with appcontext.Register()
function (or just sending signal to itself, maybe that was already the plan). I'm not 100% against removing appcontext
but if it works and is reusable then I don't think we should duplicate its functionality.
Not future, the changes are implemented on the CLI/CLI plugin lib side and the only change needed on this side is to use cobra's
I'm not sure I understand what this means.
If the issue is a stylistic one, I personally don't see this as much different from the The important change here is to use the cobra's |
You don't need to assign it for each command. Just once in root command would do and all commands pick this up automatically. I'll look up the CLI plugin lib update. |
Yes, this has already been vendored in https:/docker/buildx/blame/ba43fe08f43a7239b124fc12e158843a23ca711d/vendor/github.com/docker/cli/cli-plugins/plugin/plugin.go#L84 . That kind of context injection would work well with
Just
but not needed. Current vendored code will work well and is better as the plugin signal count is unaffected. |
Signed-off-by: Laura Brehm <[email protected]>
See docker/cli#4599 and docker/cli#4769. Since we switched to using the cobra command context instead of `appcontext`, we need to set up the signal handling that was being provided by `appcontext`, as well as configuring the context with the OTEL tracing utilities also used by `appcontext`. This commit introduces `cobrautil.ConfigureContext` which implements the pre-existing signal handling logic from `appcontext` and cancels the command's context when a signal is received, as well as doing the relevant OTEL config. Signed-off-by: Laura Brehm <[email protected]>
5b2ecca
to
e60182b
Compare
Signed-off-by: Tonis Tiigi <[email protected]>
e60182b
to
147c713
Compare
Thanks for taking over while I was out @tonistiigi. LGTM from a quick look, but I'll also take a better look later |
LGTM, feel free to rebase/merge @tonistiigi (and merge 👀 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after manual testing for untested os (macos/windows). Hopefully #2206 should help catching regression in the future but would need integration tests for these platforms as well for client side behavior (will work on this in follow-up)
docker/cli#4599 made it so that we now have a job control mechanism for the CLI to signal running plugin processes that they should exit (via cancelling the executing
command.Context
).This PR incorporates those changes in the CLI by:
bumping the CLI version to bring in the plugin-side code for this changeappcontext
package, since we're now relying on cobra'scommand.Context()
to know when to cancelcobrautil.ConfigureContext
, which sets up the signal handling logic that was previously being provided byappcontext
(and cancels the commands context when we get signalled) as well as calling the OTEL tracing utilities for the command's context that we're now using.I've made similar changes in Compose in docker/compose#11292.
I tried to test most cases and these changes seemed to work okay, but please give it a thorough look and test them.