Forward SIGTERM and handle that as an interrupt

This commit is contained in:
Mitchell Hashimoto 2016-12-08 12:10:52 -05:00
parent 9c80c82d9e
commit e9c35eae32
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
6 changed files with 25 additions and 10 deletions

View File

@ -223,7 +223,8 @@ func makeShutdownCh() <-chan struct{} {
resultCh := make(chan struct{}) resultCh := make(chan struct{})
signalCh := make(chan os.Signal, 4) signalCh := make(chan os.Signal, 4)
signal.Notify(signalCh, interruptSignals...) signal.Notify(signalCh, ignoreSignals...)
signal.Notify(signalCh, forwardSignals...)
go func() { go func() {
for { for {
<-signalCh <-signalCh

View File

@ -60,7 +60,8 @@ func realMain() int {
wrapConfig.Handler = panicHandler(logTempFile) wrapConfig.Handler = panicHandler(logTempFile)
wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter) wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
wrapConfig.Stdout = outW wrapConfig.Stdout = outW
wrapConfig.IgnoreSignals = interruptSignals wrapConfig.IgnoreSignals = ignoreSignals
wrapConfig.ForwardSignals = forwardSignals
exitStatus, err := panicwrap.Wrap(&wrapConfig) exitStatus, err := panicwrap.Wrap(&wrapConfig)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err) fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err)

View File

@ -7,7 +7,5 @@ import (
"syscall" "syscall"
) )
var interruptSignals []os.Signal = []os.Signal{ var ignoreSignals = []os.Signal{os.Interrupt}
os.Interrupt, var forwardSignals = []os.Signal{syscall.SIGTERM}
syscall.SIGTERM,
}

View File

@ -6,4 +6,5 @@ import (
"os" "os"
) )
var interruptSignals []os.Signal = []os.Signal{os.Interrupt} var ignoreSignals = []os.Signal{os.Interrupt}
var forwardSignals []os.Signal

View File

@ -68,6 +68,13 @@ type WrapConfig struct {
// Catch and igore these signals in the parent process, let the child // Catch and igore these signals in the parent process, let the child
// handle them gracefully. // handle them gracefully.
IgnoreSignals []os.Signal IgnoreSignals []os.Signal
// Catch these signals in the parent process and manually forward
// them to the child process. Some signals such as SIGINT are usually
// sent to the entire process group so setting it isn't necessary. Other
// signals like SIGTERM are only sent to the parent process and need
// to be forwarded. This defaults to empty.
ForwardSignals []os.Signal
} }
// BasicWrap calls Wrap with the given handler function, using defaults // BasicWrap calls Wrap with the given handler function, using defaults
@ -166,16 +173,23 @@ func Wrap(c *WrapConfig) (int, error) {
// Listen to signals and capture them forever. We allow the child // Listen to signals and capture them forever. We allow the child
// process to handle them in some way. // process to handle them in some way.
sigCh := make(chan os.Signal) sigCh := make(chan os.Signal)
fwdSigCh := make(chan os.Signal)
if len(c.IgnoreSignals) == 0 { if len(c.IgnoreSignals) == 0 {
c.IgnoreSignals = []os.Signal{os.Interrupt} c.IgnoreSignals = []os.Signal{os.Interrupt}
} }
signal.Notify(sigCh, c.IgnoreSignals...) signal.Notify(sigCh, c.IgnoreSignals...)
signal.Notify(sigCh, c.ForwardSignals...)
go func() { go func() {
defer signal.Stop(sigCh) defer signal.Stop(sigCh)
defer signal.Stop(fwdSigCh)
for { for {
select { select {
case <-doneCh: case <-doneCh:
return return
case s := <-fwdSigCh:
if cmd.Process != nil {
cmd.Process.Signal(s)
}
case <-sigCh: case <-sigCh:
} }
} }

6
vendor/vendor.json vendored
View File

@ -2079,10 +2079,10 @@
"revision": "314aad379a39f6ad5bcca278e6757d9abbb3a52e" "revision": "314aad379a39f6ad5bcca278e6757d9abbb3a52e"
}, },
{ {
"checksumSHA1": "kTntIB9SdU1NsCqKwDkUr99qaj0=", "checksumSHA1": "AykrbOR+O+Yp6DQHfwe31+iyFi0=",
"path": "github.com/mitchellh/panicwrap", "path": "github.com/mitchellh/panicwrap",
"revision": "fde185d0dfb5ecac6e6b201e8855da798ebcd76f", "revision": "ba9e1a65e0f7975f055d50a2c0201c50d941c24c",
"revisionTime": "2016-11-21T18:34:54Z" "revisionTime": "2016-12-08T17:03:02Z"
}, },
{ {
"path": "github.com/mitchellh/prefixedio", "path": "github.com/mitchellh/prefixedio",