Skip to content

Commit 39f4f3e

Browse files
committed
chore: cleanup baseCtx and ctx usage
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
1 parent 1748c60 commit 39f4f3e

4 files changed

Lines changed: 8 additions & 28 deletions

File tree

cli/command/container/attach.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, o
107107
sigc := notifyAllSignals()
108108
// since the ForwardAllSignals already registers its own signal handler
109109
// and should notify the daemon independently of the clients ctx cancellation
110-
// we should use a background context to avoid the ForwardAllSignals from
110+
// we should use the context without cancellation to avoid the ForwardAllSignals from
111111
// returning before all signals are forwarded to the daemon
112-
bgCtx, cancel := context.WithCancel(context.Background())
113-
defer cancel()
112+
bgCtx := context.WithoutCancel(ctx)
114113
go ForwardAllSignals(bgCtx, apiClient, containerID, sigc)
115114
defer signal.StopCatch(sigc)
116115
}

cli/command/container/run.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,9 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
152152
sigc := notifyAllSignals()
153153
// since the ForwardAllSignals already registers its own signal handler
154154
// and should notify the daemon independently of the clients ctx cancellation
155-
// we should use a background context to avoid the ForwardAllSignals from
155+
// we should use the context without cancellation to avoid the ForwardAllSignals from
156156
// returning before all signals are forwarded to the daemon
157-
bgCtx, cancel := context.WithCancel(context.Background())
158-
defer cancel()
157+
bgCtx := context.WithoutCancel(ctx)
159158
go ForwardAllSignals(bgCtx, apiClient, containerID, sigc)
160159
defer signal.StopCatch(sigc)
161160
}

cli/command/container/signals.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,7 @@ import (
1414
//
1515
// The channel you pass in must already be setup to receive any signals you want to forward.
1616
func ForwardAllSignals(ctx context.Context, apiClient client.ContainerAPIClient, cid string, sigc <-chan os.Signal) {
17-
var (
18-
s os.Signal
19-
ok bool
20-
)
21-
for {
22-
select {
23-
case s, ok = <-sigc:
24-
if !ok {
25-
return
26-
}
27-
case <-ctx.Done():
28-
return
29-
}
30-
17+
for s := range sigc {
3118
if s == signal.SIGCHLD || s == signal.SIGPIPE {
3219
continue
3320
}

cmd/docker/docker.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ import (
2828
)
2929

3030
func main() {
31-
baseCtx, cancel := context.WithCancel(context.Background())
32-
defer cancel()
33-
34-
ctx, cancelNotify := signal.NotifyContext(baseCtx, platformsignals.TerminationSignals...)
31+
ctx, cancelNotify := signal.NotifyContext(context.Background(), platformsignals.TerminationSignals...)
3532
defer cancelNotify()
3633

3734
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
@@ -273,11 +270,8 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
273270
// or EBADF; but that is fine for our purposes.
274271
_ = srv.Close()
275272

276-
// If we're still running after 3 interruptions
277-
// (SIGINT/SIGTERM), send a SIGKILL to the plugin as a
278-
// final attempt to terminate, and exit.
273+
// force the process to terminate if it hasn't already
279274
if force {
280-
_, _ = fmt.Fprint(dockerCli.Err(), "got 3 SIGTERM/SIGINTs, forcefully exiting\n")
281275
_ = plugincmd.Process.Kill()
282276
os.Exit(1)
283277
}
@@ -301,6 +295,7 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
301295
// final attempt to terminate, and exit.
302296
if retries >= exitLimit {
303297
force = true
298+
_, _ = fmt.Fprint(dockerCli.Err(), "got 3 SIGTERM/SIGINTs, forcefully exiting\n")
304299
}
305300
tryTerminatePlugin(force)
306301
}

0 commit comments

Comments
 (0)