Skip to content

improve passing context - #4774

Merged
Benehiko merged 1 commit into
docker:masterfrom
thaJeztah:pass_context
Apr 25, 2024
Merged

improve passing context#4774
Benehiko merged 1 commit into
docker:masterfrom
thaJeztah:pass_context

Conversation

@thaJeztah

@thaJeztahthaJeztah commented Jan 11, 2024

Copy link
Copy Markdown
Member

Relates to OTEL changes and graceful exit changes

See below PRs

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@codecov-commenter

codecov-commenter commented Jan 11, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 0% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 61.08%. Comparing base (8651906) to head (709df33).

Additional details and impacted files
@@ Coverage Diff @@## master #4774 +/- ##
=======================================
Coverage 61.08% 61.08% =======================================
Files 295 295 Lines 20660 20660 =======================================
Hits 12621 12621 Misses 7142 7142 Partials 897 897 

@thaJeztah
thaJeztahforce-pushed the pass_context branch 4 times, most recently from eaa6886 to ce92d38CompareJanuary 11, 2024 21:52
@thaJeztahthaJeztah self-assigned this Jan 12, 2024
@thaJeztah
thaJeztahforce-pushed the pass_context branch 2 times, most recently from f60ef7e to 834e912CompareJanuary 30, 2024 16:16
@thaJeztahthaJeztah changed the title [WIP] improve passing contextimprove passing contextFeb 21, 2024
@thaJeztah
thaJeztah marked this pull request as ready for review February 21, 2024 17:05
@thaJeztah

Copy link
Copy Markdown
MemberAuthor

Let me move this one out of draft, but please have a close look to see if the approach makes sense. I recall I was fiddling a bit with passing the context with the combination of closures and call-sites of different parts of the code.

Comment threadcmd/docker/builder_test.go Outdated
Comment threadcmd/docker/builder_test.go Outdated
Comment threadcmd/docker/docker_test.go Outdated

@BenehikoBenehiko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are still a few places this can be improved, especially on setting context in the dockerCLI.
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))

I can also move this PR forward if you'd like me to since I am working on adding more context to the docker client in moby and adding support in the CLI for it.

Comment threadcmd/docker/builder_test.go Outdated
Comment threadcli-plugins/plugin/plugin.go Outdated

@laurazardlaurazard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@laurazard

Copy link
Copy Markdown
Member

@thaJeztah can you TA(nother)L?

Comment threadcli-plugins/plugin/plugin.go Outdated
vvoland
vvoland previously requested changes Mar 14, 2024

@vvolandvvoland left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving request changes for now; I don't think we should merge this for v26 since it's a breaking change to the plugin.Run function.

@vvolandvvoland added the kind/refactor PR's that refactor, or clean-up code label Mar 14, 2024
@Benehiko
Benehikoforce-pushed the pass_context branch 2 times, most recently from 6ae4186 to 0c8869fCompareApril 10, 2024 08:30
Comment threadcli-plugins/plugin/plugin.go Outdated
otel.SetErrorHandler(debug.OTELErrorHandler)

dockerCli, err := command.NewDockerCli()
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do plugins add their own ctx to the dockerCli with the makeCmd?

@krissettokrissettoApr 10, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah this will be breaking for plugins. I'll see what I can do to work around that.

but watch out since it already handles signals

that's fine, this PR doesn't do any signal handling here, but this is a good point and will work around this

Comment threadcmd/docker/docker.go
// which remain.
cmd.SetArgs(args)
err = cmd.Execute()
err = cmd.ExecuteContext(ctx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take note, instead of passing ctx to the root command through convoluted function calls I've cleaned it up a bit by instead passing context to cobra when we call ExecuteContext. This should only affect the CLI and not any plugins that are run through the CLI.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, this was one of the things to look into!

(Still catching up on latest changes in this PR, but yes, this was one where my approach was probably not right)

Comment threadcli/cobra.go Outdated
// setupCommonRootCommand contains the setup common to
// SetupRootCommand and SetupPluginRootCommand.
func setupCommonRootCommand(ctx context.Context, rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) {
rootCmd.SetContext(ctx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this here since we can use the cobra ExecuteContext instead. This prevents us from passing in ctx to the plugins through RunPlugin.

@Benehiko
Benehikoforce-pushed the pass_context branch 2 times, most recently from 0aad14d to 8278828CompareApril 24, 2024 08:43
@Benehiko
Benehiko dismissed vvoland’s stale reviewApril 24, 2024 09:19

v26.1.0 has been released

@krissettokrissetto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment threadcli-plugins/plugin/plugin.go Outdated
Comment threadcmd/docker/docker.go Outdated
err = cmd.Execute()
err = cmd.ExecuteContext(ctx)
if err != nil {
return err

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this get borked during a rebase? It's returning early, should be returning after hooks get called I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah true! will fix

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Should it still handle context errors early though? (cancelled context? or timeout?) I guess in that case the hooks should not be called?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to do it regardless in any case here? The plugin can decide if it wants to print something or not. Even if it's cancelled, a hook might want to add some hint/explain something.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure if a cancelled context should still be used; isn't context cancelation basically; "cancel my request" (on the first possible occasion) ? In some cases that may mean some work is needed to complete the transaction (flush to disk, e.g.) but calling hooks (and execute other binaries) feels like that should not be part of that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's valid, sure. @Benehiko wdyt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmh i'm not sure if hooks should be called after a cancellation, it'd feel wrong to me

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have too much opinion on this either way. We currently execute the hooks even when the cobra Execute function returns an error (which is a similar thing to context cancellation).

Cancelling the context early will still execute the cobra commands, but the commands checking for context cancellation will just return (with maybe an error?).

See below an example of a cobra command not acting on the context cancellation. Even if it was acting and returning an error it will still execute.

package main
import (
"context""fmt""github.com/spf13/cobra"
)
funcmain() {
ctx, cancel:=context.WithCancel(context.Background())
rootCmd:=&cobra.Command{
Use: "",
RunE: func(cmd*cobra.Command, args []string) error {
cmd.Help()
fmt.Printf("Cobra command ctx: %v\n", cmd.Context().Err())
returnnil
},
}
cancel()
fmt.Printf("Before Cobra ctx: %v\n", ctx.Err())
err:=rootCmd.ExecuteContext(ctx)
fmt.Printf("%v\n", err)
}
>~/G/test-cobra go run . 09:14:06
Before Cobra ctx: context canceled
Usage:
[flags]
Flags:
-h, --help helpfor this command
Cobra command ctx: context canceled
<nil>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See here what the code currently looks like on the master branch
https://github.com/docker/cli/blob/master/cmd/docker/docker.go#L355-L367

@laurazardlaurazard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vvoland

vvoland commented Apr 25, 2024

Copy link
Copy Markdown
Collaborator

Ah, one more thing, can you please add the changelog description with highlighting the API change?

EDIT: Sorry, I confused PRs 🙈 There was no API change in the end here (we didn't alter the RunPlugin signature), so no need for changelog!

Explicitly create the context and set it on the CLI, instead of depending on
NewDockerCli() to instance a default context.
Co-authored-by: Sebastiaan van Stijn <github@gone.nl>
Co-authored-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
@Benehiko
Benehiko merged commit 7f15dfa into docker:masterApr 25, 2024
@thaJeztah
thaJeztah deleted the pass_context branch April 25, 2024 14:45
@Benehiko

Copy link
Copy Markdown
Member

Ah, one more thing, can you please add the changelog description with highlighting the API change?

EDIT: Sorry, I confused PRs 🙈 There was no API change in the end here (we didn't alter the RunPlugin signature), so no need for changelog!

@vvoland You had the PR right, I just reverted those changes in the PR and squashed the commits

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

Labels

area/apiarea/pluginskind/refactorPR's that refactor, or clean-up codestatus/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@thaJeztah@codecov-commenter@laurazard@vvoland@Benehiko@krissetto