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

cmd/docker: add cause to user-terminated context.Context #5760

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Benehiko
Copy link
Member

This patch adds a "cause" to the context.Context error when the user terminates the process through SIGINT/SIGTERM.

This allows us to distinguish the cause of the
context.Context cancellation. In future we would also be able to improve the UX of printed errors
based on the underlying cause.

- What I did

- How I did it

- How to verify it

- Description for the changelog

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

This patch adds a "cause" to the `context.Context`
error when the user terminates the process through
SIGINT/SIGTERM.

This allows us to distinguish the cause of the
`context.Context` cancellation. In future we would
also be able to improve the UX of printed errors
based on the underlying cause.

Signed-off-by: Alano Terblanche <[email protected]>
@Benehiko Benehiko requested a review from a team January 20, 2025 11:06
@codecov-commenter
Copy link

codecov-commenter commented Jan 20, 2025

Codecov Report

Attention: Patch coverage is 69.56522% with 7 lines in your changes missing coverage. Please review.

Project coverage is 59.43%. Comparing base (f97ec69) to head (bdd630c).
Report is 17 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5760      +/-   ##
==========================================
- Coverage   59.47%   59.43%   -0.04%     
==========================================
  Files         346      347       +1     
  Lines       29367    29422      +55     
==========================================
+ Hits        17465    17488      +23     
- Misses      10929    10962      +33     
+ Partials      973      972       -1     

if err != nil && !errdefs.IsCancelled(err) {
ctx := context.Background()
err := dockerMain(ctx)
if err != nil && !errdefs.IsCancelled(err) && !errors.Is(err, errCtxUserTerminated) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

errors.Is(err, errCtxUserTerminated) will never be true. CancelCauseFunc sets the cause of the context error and not the error itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

ctx errors are propagated up from the stack as normal errors. L35 will contain the context error making the check for errors.Is(err, errCtxUserTerminated) true.

But you are correct that in this PR it won't ever be true since we are incorrectly wrapping errors downstream. A fix for that is in this PR #5666.

if err != nil && !errdefs.IsCancelled(err) {
ctx := context.Background()
err := dockerMain(ctx)
if err != nil && !errdefs.IsCancelled(err) && !errors.Is(err, errCtxUserTerminated) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I'm wondering. In which case would err be IsCancelled and not caused by L54?

Copy link
Member Author

Choose a reason for hiding this comment

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

When we cancel a prompt it returns an IsCancelled error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But isn't the prompt cancellation always a direct consequence of SIGINT?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, but it was wired up to replace the error from context.Cancelled to errdefs.Cancelled
https://github.com/docker/cli/blob/master/cli/command/utils.go#L114-L117
https://github.com/docker/cli/blob/master/cli/command/utils.go#L78

@Benehiko Benehiko requested review from ndeloof and glours January 21, 2025 14:35
signal.Stop(ch)
return
case <-ch:
cancel(errCtxUserTerminated)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The thing that I can't wrap my mind around is:

After this cancel, we'll have:

ctx.Err() = context.Canceled
context.Cause(ctx) = errCtxUserTerminated

With that in mind, how dockerMain ends up returning the cause, instead of the ctx.Err()? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes you are right, essentially right now not all commands would return a cause, but the standard library should - like http.NewRequestWithContext.

Copy link
Member Author

Choose a reason for hiding this comment

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

fayazkhan121

This comment was marked as spam.

Signed-off-by: Alano Terblanche <[email protected]>
@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from 8839f12 to bdd630c Compare January 24, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants