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

Merged
merged 1 commit into from
Feb 5, 2025

Conversation

Benehiko
Copy link
Member

@Benehiko Benehiko commented Jan 20, 2025

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

`docker` cli now sets the corresponding exit code when the process was killed with a signal

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

@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 57.89474% with 16 lines in your changes missing coverage. Please review.

Project coverage is 59.43%. Comparing base (f97ec69) to head (c51be77).
Report is 105 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    29431      +64     
==========================================
+ Hits        17465    17492      +27     
- Misses      10929    10965      +36     
- Partials      973      974       +1     

@Benehiko Benehiko requested review from ndeloof and glours January 21, 2025 14:35
fayazkhan121

This comment was marked as spam.

@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from 8839f12 to bdd630c Compare January 24, 2025 15:32
Copy link

@fayazkhan121 fayazkhan121 left a comment

Choose a reason for hiding this comment

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

The test looks solid. TestUserTerminatedError test case properly introduces notifyContext and handles the termination signals as expected.

@Benehiko
Copy link
Member Author

Please don't merge yet, this is incorrect. We need to have status 130 print when a container exits with that code.

@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from da5701e to bdd630c Compare January 31, 2025 12:44
@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from 577e0d2 to 4e7cf34 Compare February 3, 2025 13:06
@thaJeztah thaJeztah dismissed fayazkhan121’s stale review February 3, 2025 13:08

Spam / AI account (blocked now)

@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from 4e7cf34 to 8e41a36 Compare February 3, 2025 13:09
@vvoland
Copy link
Collaborator

vvoland commented Feb 3, 2025

LGTM overall, but we probably still need to adjust the output, see:

Before

$ docker run --rm -it --pull=always fedora
latest: Pulling from library/fedora
e3c408ecb0c2: Downloading [====================================>              ]  41.94MB/57.21MB
^Cdocker: context canceled.
See 'docker run --help'.

After

$  ./build/docker run --rm -it --pull=always fedora
latest: Pulling from library/fedora
e3c408ecb0c2: Downloading [======================>                            ]  26.21MB/57.21MB
^Cdocker: 
Run 'docker run --help' for more information

IMO, it shouldn't print anything at all, so it should look like:

$ docker run --rm -it --pull=always fedora
latest: Pulling from library/fedora
e3c408ecb0c2: Downloading [====================================>              ]  41.94MB/57.21MB
^C

@Benehiko
Copy link
Member Author

Benehiko commented Feb 3, 2025

@vvoland we won't print anything when this PR #5778 rebases with these changes

@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from 8e41a36 to 9f41558 Compare February 3, 2025 15:23
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]>

cmd/docker: fix possible race between ctx channel and signal channel

Signed-off-by: Alano Terblanche <[email protected]>

test: notifyContext

Signed-off-by: Alano Terblanche <[email protected]>

cmd/docker: print status on SIGTERM and not SIGINT

Signed-off-by: Alano Terblanche <[email protected]>
@Benehiko Benehiko force-pushed the user-terminated-ctx-err branch from 9f41558 to c51be77 Compare February 3, 2025 15:29
@vvoland vvoland added the kind/bugfix PR's that fix bugs label Feb 3, 2025
Copy link
Collaborator

@vvoland vvoland left a comment

Choose a reason for hiding this comment

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

LGTM

@vvoland vvoland requested a review from thaJeztah February 3, 2025 15:35
@vvoland vvoland requested a review from laurazard February 3, 2025 15:36
Comment on lines +31 to +37
type errCtxSignalTerminated struct {
signal os.Signal
}

func (e errCtxSignalTerminated) Error() string {
return ""
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a small nit/suggestion, not necessary.

You could do:

Suggested change
type errCtxSignalTerminated struct {
signal os.Signal
}
func (e errCtxSignalTerminated) Error() string {
return ""
}
type errCtxSignalTerminated struct {
signal os.Signal
}
func (e *errCtxSignalTerminated) Error() string {
return ""
}
func (e *errCtxSignalTerminated) exitCode() int {
if e == nil {
return 1
}
s, ok := e.signal.(syscall.Signal)
if !ok {
return 1
}
return 128 + int(s)
}

Then errCtxSignalTerminated implements error (instead of *errCtxSignalTerminated implementing error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 on the exitCode

Copy link
Member Author

Choose a reason for hiding this comment

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

The suggestion looks alright to me, although we only use the exit code inside the getExitCode function and this error type is only used inside cmd/docker/docker.go. At least to me, it just seems like extra refactoring with little benefit.

@Benehiko Benehiko merged commit 9005f36 into docker:master Feb 5, 2025
95 checks passed
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.

6 participants