From df86cb9a5c949530336b43100b303876f07c69ba Mon Sep 17 00:00:00 2001 From: unclejack Date: Sat, 20 Jul 2013 13:47:13 +0300 Subject: [PATCH 1/4] make docker run handle SIGINT/SIGTERM --- commands.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/commands.go b/commands.go index f0e1695b3f..db8a126696 100644 --- a/commands.go +++ b/commands.go @@ -1393,6 +1393,21 @@ func (cli *DockerCli) CmdRun(args ...string) error { v.Set("stderr", "1") } + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + go func() { + for { + sig := <-signals + if sig == syscall.SIGINT || sig == syscall.SIGTERM { + fmt.Printf("\nReceived signal: %s; cleaning up\n", sig) + if err := cli.CmdStop("-t", "4", runResult.ID); err != nil { + fmt.Printf("failed to stop container:", err) + } + return + } + } + }() + if err := cli.hijack("POST", "/containers/"+runResult.ID+"/attach?"+v.Encode(), config.Tty, cli.in, cli.out); err != nil { utils.Debugf("Error hijack: %s", err) return err From 88cb9f3116e41b00b00fdccf6359a555e87061bd Mon Sep 17 00:00:00 2001 From: unclejack Date: Fri, 9 Aug 2013 20:33:17 +0300 Subject: [PATCH 2/4] keep processing signals after the first one --- commands.go | 1 - 1 file changed, 1 deletion(-) diff --git a/commands.go b/commands.go index db8a126696..d045625c73 100644 --- a/commands.go +++ b/commands.go @@ -1403,7 +1403,6 @@ func (cli *DockerCli) CmdRun(args ...string) error { if err := cli.CmdStop("-t", "4", runResult.ID); err != nil { fmt.Printf("failed to stop container:", err) } - return } } }() From 2ba5c915473ce6fe769fb059db4120e2a21fb42e Mon Sep 17 00:00:00 2001 From: unclejack Date: Fri, 9 Aug 2013 23:23:27 +0300 Subject: [PATCH 3/4] minor cleanup for signal handling --- commands.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index d045625c73..ffe4ce230e 100644 --- a/commands.go +++ b/commands.go @@ -1396,13 +1396,10 @@ func (cli *DockerCli) CmdRun(args ...string) error { signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) go func() { - for { - sig := <-signals - if sig == syscall.SIGINT || sig == syscall.SIGTERM { - fmt.Printf("\nReceived signal: %s; cleaning up\n", sig) - if err := cli.CmdStop("-t", "4", runResult.ID); err != nil { - fmt.Printf("failed to stop container:", err) - } + for sig := range signals { + fmt.Printf("\nReceived signal: %s; cleaning up\n", sig) + if err := cli.CmdStop("-t", "4", runResult.ID); err != nil { + fmt.Printf("failed to stop container:", err) } } }() From 641ddaeb03f8b8eee5c1ca11e3024976378ceb6d Mon Sep 17 00:00:00 2001 From: unclejack Date: Fri, 9 Aug 2013 23:27:34 +0300 Subject: [PATCH 4/4] add formatting directive to failure to stop container error --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index ffe4ce230e..85b5c9abe9 100644 --- a/commands.go +++ b/commands.go @@ -1399,7 +1399,7 @@ func (cli *DockerCli) CmdRun(args ...string) error { for sig := range signals { fmt.Printf("\nReceived signal: %s; cleaning up\n", sig) if err := cli.CmdStop("-t", "4", runResult.ID); err != nil { - fmt.Printf("failed to stop container:", err) + fmt.Printf("failed to stop container: %v", err) } } }()