diff --git a/signals.go b/signals.go index a992dc4abeb..a195d801145 100644 --- a/signals.go +++ b/signals.go @@ -18,14 +18,23 @@ const signalBufferSize = 2048 // while still forwarding all other signals to the process. // If notifySocket is present, use it to read systemd notifications from the container and // forward them to notifySocketHost. -func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) chan *signalHandler { +func newSignalHandler(enableSubreaper bool, detach bool, notifySocket *notifySocket) chan *signalHandler { if enableSubreaper { // set us as the subreaper before registering the signal handler for the container if err := system.SetSubreaper(1); err != nil { logrus.Warn(err) } } - handler := make(chan *signalHandler) + handler := make(chan *signalHandler, 1) + // For detached container, we don't need to setup signal notifier, because + // there is no customer to consume the signals in `forward()`. + if detach { + handler <- &signalHandler{ + signals: nil, + notifySocket: notifySocket, + } + return handler + } // signal.Notify is actually quite expensive, as it has to configure the // signal mask and add signal handlers for all signals (all ~65 of them). // So, defer this to a background thread while doing the rest of the io/tty diff --git a/utils_linux.go b/utils_linux.go index 7de93340a94..8a24850fcf2 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -252,7 +252,7 @@ func (r *runner) run(config *specs.Process) (int, error) { // Setting up IO is a two stage process. We need to modify process to deal // with detaching containers, and then we get a tty after the container has // started. - handlerCh := newSignalHandler(r.enableSubreaper, r.notifySocket) + handlerCh := newSignalHandler(detach, r.enableSubreaper, r.notifySocket) tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket) if err != nil { return -1, err