Skip to content

Commit 688de6d

Browse files
authored
Merge pull request #4769 from laurazard/signal-handling-fix-tty
plugins: run plugin with new process group ID
2 parents ad12276 + ef5e5fa commit 688de6d

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

cli-plugins/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func PluginRunCommand(dockerCli command.Cli, name string, rootcmd *cobra.Command
233233
cmd.Stdin = os.Stdin
234234
cmd.Stdout = os.Stdout
235235
cmd.Stderr = os.Stderr
236+
configureOSSpecificCommand(cmd)
236237

237238
cmd.Env = os.Environ()
238239
cmd.Env = append(cmd.Env, ReexecEnvvar+"="+os.Args[0])

cli-plugins/manager/manager_unix.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22

33
package manager
44

5+
import (
6+
"os/exec"
7+
"syscall"
8+
)
9+
510
var defaultSystemPluginDirs = []string{
611
"/usr/local/lib/docker/cli-plugins", "/usr/local/libexec/docker/cli-plugins",
712
"/usr/lib/docker/cli-plugins", "/usr/libexec/docker/cli-plugins",
813
}
14+
15+
func configureOSSpecificCommand(cmd *exec.Cmd) {
16+
// Spawn the plugin process in a new process group, so that signals are not forwarded by the OS.
17+
// The foreground process group is e.g. sent a SIGINT when Ctrl-C is input to the TTY, but we
18+
// implement our own job control for the plugin.
19+
cmd.SysProcAttr = &syscall.SysProcAttr{
20+
Setpgid: true,
21+
}
22+
}

cli-plugins/manager/manager_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package manager
22

33
import (
44
"os"
5+
"os/exec"
56
"path/filepath"
67
)
78

89
var defaultSystemPluginDirs = []string{
910
filepath.Join(os.Getenv("ProgramData"), "Docker", "cli-plugins"),
1011
filepath.Join(os.Getenv("ProgramFiles"), "Docker", "cli-plugins"),
1112
}
13+
14+
func configureOSSpecificCommand(cmd *exec.Cmd) {
15+
// no-op
16+
}

cmd/docker/docker.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,16 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string,
240240
// we send a SIGKILL to the plugin process and exit
241241
go func() {
242242
retries := 0
243-
for range signals {
243+
for s := range signals {
244244
if conn != nil {
245245
if err := conn.Close(); err != nil {
246246
_, _ = fmt.Fprintf(dockerCli.Err(), "failed to signal plugin to close: %v\n", err)
247247
}
248248
conn = nil
249+
} else {
250+
// When the plugin is communicating via socket with the host CLI, we perform job control via the socket.
251+
// However, if the plugin is an old version that is not socket-aware, we need to explicitly forward termination signals.
252+
plugincmd.Process.Signal(s)
249253
}
250254
retries++
251255
if retries >= exitLimit {

0 commit comments

Comments
 (0)