Skip to content

feat: global signal handling to cancel ctx for graceful exits - #4993

Merged
Benehiko merged 1 commit into
docker:masterfrom
Benehiko:hoist-signal-from-prompt
Jun 10, 2024
Merged

feat: global signal handling to cancel ctx for graceful exits#4993
Benehiko merged 1 commit into
docker:masterfrom
Benehiko:hoist-signal-from-prompt

Conversation

@Benehiko

Copy link
Copy Markdown
Member

Since other repositories import from the CLI, the signal termination handling done inside the PromptForConfirmation could cause problems for the third party repositories. Thus this PR hoists the signal termination out of the PromptForConfirmation function.

- What I did
Hoist the signal handling from the PromptForConfirmation function to the more appropriate top-level function runDocker.

- How I did it
Using the signal package I catch context cancellations as well as termination signals then pass the returned context to cobra's Execute method.

- How to verify it
Tests

- Description for the changelog

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

@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch from 8e2f328 to 13e4776CompareApril 8, 2024 08:19
@codecov-commenter

codecov-commenter commented Apr 8, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 7.69231% with 36 lines in your changes missing coverage. Please review.

Project coverage is 61.83%. Comparing base (8b924a5) to head (3f0d90a).
Report is 23 commits behind head on master.

Additional details and impacted files
@@ Coverage Diff @@## master #4993 +/- ##
==========================================
+ Coverage 61.37% 61.83% +0.46% 
==========================================
Files 298 298 Lines 20717 20736 +19 ==========================================
+ Hits 12715 12823 +108 + Misses 7102 7000 -102 - Partials 900 913 +13 

@Benehiko

Copy link
Copy Markdown
MemberAuthor

Thinking about this now, maybe I should just do this top-level inside the main function and then rework the plugin signal handling as well to accept a context.Done() instead.

@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch 2 times, most recently from 63a2225 to 47b6915CompareApril 9, 2024 14:03
Comment threadcmd/docker/docker.go
Comment threadcmd/docker/docker.go
@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch from 47b6915 to 7ffda3eCompareApril 10, 2024 07:50
@Benehiko

Benehiko commented Apr 10, 2024

Copy link
Copy Markdown
MemberAuthor

This PR is blocked by moby/moby#47536. The cobra context isn't given to the prompt function when plugin install is called due to acceptPrivileges not accepting a context https://github.com/docker/cli/blob/master/cli/command/plugin/install.go#L145

Also need to update and merge #4774

@laurazard

Copy link
Copy Markdown
Member

Looks like the linter's complaining, and some tests are broken because now the output from the graceful termination logic is different:

=== FAIL: e2e/cli-plugins TestPluginSocketCommunication/detached/the_main_CLI_exits_after_3_signals (1.10s)
socket_test.go:232: assertion failed: --- ←
+++ →
@@ -1,2 +1,2 @@
-got 2 SIGTERM/SIGINTs, forcefully exiting
+got 3 SIGTERM/SIGINTs, forcefully exiting

I think it should be fine to hardcode the 3 in that print, since that's the total number of signals we've received (counting the one that cancelled the context. It might also be possible to reword it some other way 😅


I guess we'll hold off on merging this one since it needs changes to moby that we want to get in after 26.1, so we can get it in after we cut a release.

@Benehiko

Copy link
Copy Markdown
MemberAuthor

Looks like the linter's complaining, and some tests are broken because now the output from the graceful termination logic is different:

=== FAIL: e2e/cli-plugins TestPluginSocketCommunication/detached/the_main_CLI_exits_after_3_signals (1.10s)
socket_test.go:232: assertion failed: --- ←
+++ →
@@ -1,2 +1,2 @@
-got 2 SIGTERM/SIGINTs, forcefully exiting
+got 3 SIGTERM/SIGINTs, forcefully exiting

I think it should be fine to hardcode the 3 in that print, since that's the total number of signals we've received (counting the one that cancelled the context. It might also be possible to reword it some other way 😅

I guess we'll hold off on merging this one since it needs changes to moby that we want to get in after 26.1, so we can get it in after we cut a release.

Ah yeah, the tests won't pass right now. Not without the changes to moby and context passing in CLI.

I'll hardcode the 3 :)

@laurazardlaurazard added this to the 27.0.0 milestone Apr 18, 2024
@BenehikoBenehiko mentioned this pull request Apr 25, 2024
@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch from 7ffda3e to 743fc70CompareApril 25, 2024 14:41
@BenehikoBenehiko changed the title fix: hoist signal handling from prompt confirmationfeat: global signal handling to cancel ctx for graceful exitsApr 26, 2024
@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch from 8b74c89 to 79fdc57CompareJune 4, 2024 13:26

@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 overall, but looks like there's some tests failing/linters complaining. I wonder if were doing things correctly with those deferred cancel()s in main.

@Benehiko

Copy link
Copy Markdown
MemberAuthor

LGTM overall, but looks like there's some tests failing/linters complaining. I wonder if were doing things correctly with those deferred cancel()s in main.

I have been looking into it. I've fixed one of the tests, specifically the plugins e2e signal handling and still busy on the containers e2e

@Benehiko
Benehiko requested a review from a teamJune 6, 2024 09:12
Comment threadcli/command/container/attach.go Outdated
vvoland
vvoland previously requested changes Jun 6, 2024
Comment threadcli/command/container/attach.go Outdated
Comment threadcli/command/container/run.go Outdated
Comment threadcmd/docker/docker.go Outdated
@Benehiko

Copy link
Copy Markdown
MemberAuthor

Wondering why this test is failing on the connhelper-ssh tests only. I haven't been able to reproduce it locally. Might be a race condition of sorts...
https://github.com/docker/cli/actions/runs/9398990847/job/25885517677?pr=4993#step:5:731

@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.

overall LGTM, just a couple minor comments

Comment threadcmd/docker/docker.go Outdated
Comment threadcli/command/container/attach.go
@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch 2 times, most recently from 39f4f3e to fbbc83eCompareJune 6, 2024 13:59
Comment threadcli/command/container/run.go Outdated
Comment threadcli/command/container/attach.go Outdated
Comment threadcmd/docker/docker.go Outdated
@laurazard
laurazard requested a review from vvolandJune 7, 2024 11:25

@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.

re-LGTM

@laurazard

Copy link
Copy Markdown
Member

@vvoland can you take another look and then I'll merge?

@vvoland
vvoland dismissed their stale reviewJune 7, 2024 13:21

re-reviewing

Comment threadcmd/docker/docker.go Outdated
Comment threadcmd/docker/docker.go Outdated
@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch from e5f3765 to a9b8be1CompareJune 7, 2024 14:52
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
@Benehiko
Benehikoforce-pushed the hoist-signal-from-prompt branch from a9b8be1 to 3f0d90aCompareJune 7, 2024 14:59

@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.

LGTM

@Benehiko
Benehiko merged commit 6b58179 into docker:masterJun 10, 2024
@Benehiko
Benehiko deleted the hoist-signal-from-prompt branch June 10, 2024 11:44
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

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