Skip to content
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

Child command context is not updated when global context changed #2077

Open
libozh opened this issue Nov 23, 2023 · 2 comments
Open

Child command context is not updated when global context changed #2077

libozh opened this issue Nov 23, 2023 · 2 comments

Comments

@libozh
Copy link

libozh commented Nov 23, 2023

In command.go
func (c *Command) ExecuteC() (cmd *Command, err error) {
...
// We have to pass global context to children command
// if context is present on the parent command.
if cmd.ctx == nil {
cmd.ctx = c.ctx
}
...
}

The code doesn't match the intention of the comment. c.ctx hold the global context. Should the code be:

// We have to pass global context to children command
// if context is present on the parent command.
if c.ctx != nil {
	cmd.ctx = c.ctx
}

Without this change, if global context changed, the child command still hold the old context.

@marckhouzam
Copy link
Collaborator

Thanks @libozh

cobra/command.go

Lines 1109 to 1113 in 199b7ab

// We have to pass global context to children command
// if context is present on the parent command.
if cmd.ctx == nil {
cmd.ctx = c.ctx
}

In the above code c is the root command while cmd is the sub-command being called.
I interpret the line if cmd.ctx == nil { to say that if the sub-command does not already have a context, it gets set to the root context.

I haven't used contexts with Cobra, but my guess is that you can set a different context for a sub-command than for the root and therefore you don't want the root context to overwrite the sub-command context. So it seems reasonable to me.

Was your observation based on looking at the code or did you hit a problem with this logic?

Without this change, if global context changed, the child command still hold the old context.

I'm not sure when a context could change. I think it is set once for one program execution.

@libozh
Copy link
Author

libozh commented Dec 27, 2023

Thank you Marc for the feedback.
In my specific use case, the root command context carries cancellation signals, it will pass it to sub-command. When root command update its context, but sub-command still keeps the old context, the cancellation would fail. I think based on the "context" usage, the sub-command should stay consistent with root command. An example when root command updates its context is: there is an update for the command configuration, the command re-run automatically with a new context.
Actually when I look into the pull requests today, I find a similar pull request as above: #1875
I think it is trying to cover similar use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants