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

signals/utils: always handle received signals #11361

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

laurazard
Copy link
Member

The changes in dcbf005 fixed the "cancellable context" detection, and made it so that Compose would conditionally set up signal handling when the context was already not cancellable/when the plugin was running through the CLI, as we'd introduced a mechanism into the CLI to signal plugins to exit through a socket instead of handling signals themselves.

This had some (not noticed at the time) issues when running through the CLI as, due to sharing a process group id with the parent CLI process, when a user CTRL-Cs the CLI will notify the plugin via the socket but the plugin process itself will also be signalled if attached to the TTY. This impacted some Compose commands that don't set up signal handling - so not compose up, but other commands would immediately quit instead of getting some "graceful" cancelled output.

We initially attempted to address this "double notification" issue in the CLI by executing plugins under a new pgid so that they wouldn't be signalled, but that posed an issue with Buildx reading from the TTY, (see: moby/moby#47073) so we reverted the process group id changes and ended at a temporary solution in docker/cli#4792 where the CLI will only notify plugins via the socket when they are not already going to be signalled (when attached to a TTY).

Due to this, plugins should always set up some signal handling, which this commit implements.

What I did

Make Compose always handle signals – you can verify the broken/fixed behaviour by building Compose from this PR and the CLI from master, and then trying:

docker compose pull and hitting CTRL-C before it finishes

Pre-changes:

 ../../cli/build/docker compose pull
[+] Pulling 0/1
 ⠹ a Pulling                                                                                          0.3s
^C%

after:

 ../../cli/build/docker compose pull
[+] Pulling 1/1
 ✘ a Error                                                                                            0.2s
canceled

Related issue

(not mandatory) A picture of a cute animal, if possible in relation to what you did

@laurazard laurazard requested review from ndeloof, milas, thaJeztah and a team January 17, 2024 17:17
@laurazard laurazard self-assigned this Jan 17, 2024
@laurazard laurazard requested review from glours and jhrotko and removed request for a team January 17, 2024 17:18
The changes in
docker@dcbf005
fixed the "cancellable context" detection, and made it so that Compose
would conditionally set up signal handling when the context was already
not cancellable/when the plugin was running through the CLI, as we'd
introduced a mechanism into the CLI to signal plugins to exit through a
socket instead of handling signals themselves.

This had some (not noticed at the time) issues when running through the
CLI as, due to sharing a process group id with the parent CLI process,
when a user CTRL-Cs the CLI will notify the plugin via the socket but
the plugin process itself will also be signalled if attached to the TTY.
This impacted some Compose commands that don't set up signal handling -
so not `compose up`, but other commands would immediately quit instead
of getting some "graceful" cancelled output.

We initially attempted to address this "double notification" issue in
the CLI by executing plugins under a new pgid so that they wouldn't be
signalled, but that posed an issue with Buildx reading from the TTY,
(see: moby/moby#47073) so we reverted the
process group id changes and ended at a temporary solution in
docker/cli#4792 where the CLI will only notify
plugins via the socket when they are not already going to be signalled
(when attached to a TTY).

Due to this, plugins should always set up some signal handling, which
this commit implements.

Signed-off-by: Laura Brehm <[email protected]>
@laurazard laurazard force-pushed the always-handle-signals branch from b90209e to 898e1b6 Compare January 17, 2024 17:19
Copy link
Contributor

@milas milas left a comment

Choose a reason for hiding this comment

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

lgtm!

ctx, cancel := context.WithCancel(cmd.Context())

s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
Copy link
Contributor

Choose a reason for hiding this comment

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

fwiw I think this could be further simplified w/ signal.NotifyContext: https://pkg.go.dev/os/signal#NotifyContext

Copy link
Contributor

@milas milas left a comment

Choose a reason for hiding this comment

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

lgtm!

@laurazard
Copy link
Member Author

laurazard commented Jan 17, 2024

Also sorry about all of these changes/reverts/fixes, CLI signal handling stuff is hard and sockets are not all created equal (did you know macOS does not support abstract sockets?) 🥲😭

@laurazard
Copy link
Member Author

laurazard commented Jan 17, 2024

Ahh don't merge this, it looks like it's breaking something in run nvm, looks like a false alarm

@laurazard laurazard marked this pull request as draft January 17, 2024 17:47
Copy link

codecov bot commented Jan 17, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (3c4593f) 56.55% compared to head (898e1b6) 56.57%.
Report is 1 commits behind head on main.

Files Patch % Lines
pkg/compose/convert.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11361      +/-   ##
==========================================
+ Coverage   56.55%   56.57%   +0.02%     
==========================================
  Files         136      136              
  Lines       11541    11538       -3     
==========================================
+ Hits         6527     6528       +1     
+ Misses       4387     4383       -4     
  Partials      627      627              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@laurazard laurazard marked this pull request as ready for review January 17, 2024 22:31
@glours glours merged commit fb6d922 into docker:main Jan 18, 2024
26 checks passed
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

Successfully merging this pull request may close these issues.

4 participants