Skip to content

Commit 48beb0d

Browse files
committed
skip setup signal notifier for detached container
For detached container, we don't need to setup signal notifier, because there is no customer to consume the signals in `forward()`. Signed-off-by: lifubang <[email protected]>
1 parent 559892b commit 48beb0d

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

signals.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os/signal"
66

77
"github.com/opencontainers/runc/libcontainer"
8-
"github.com/opencontainers/runc/libcontainer/system"
98
"github.com/opencontainers/runc/libcontainer/utils"
109

1110
"github.com/sirupsen/logrus"
@@ -16,13 +15,7 @@ const signalBufferSize = 2048
1615

1716
// newSignalHandler returns a signal handler for processing SIGCHLD and SIGWINCH signals
1817
// while still forwarding all other signals to the process.
19-
func newSignalHandler(enableSubreaper bool) chan *signalHandler {
20-
if enableSubreaper {
21-
// set us as the subreaper before registering the signal handler for the container
22-
if err := system.SetSubreaper(1); err != nil {
23-
logrus.Warn(err)
24-
}
25-
}
18+
func newSignalHandler() chan *signalHandler {
2619
handler := make(chan *signalHandler)
2720
// signal.Notify is actually quite expensive, as it has to configure the
2821
// signal mask and add signal handlers for all signals (all ~65 of them).
@@ -54,13 +47,9 @@ type signalHandler struct {
5447

5548
// forward handles the main signal event loop forwarding, resizing, or reaping depending
5649
// on the signal received.
57-
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) {
50+
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty) (int, error) {
5851
// make sure we know the pid of our main process so that we can return
5952
// after it dies.
60-
if detach {
61-
return 0, nil
62-
}
63-
6453
pid1, err := process.Pid()
6554
if err != nil {
6655
return -1, err

utils_linux.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/opencontainers/runc/libcontainer"
1919
"github.com/opencontainers/runc/libcontainer/configs"
2020
"github.com/opencontainers/runc/libcontainer/specconv"
21+
"github.com/opencontainers/runc/libcontainer/system"
2122
"github.com/opencontainers/runc/libcontainer/system/kernelversion"
2223
"github.com/opencontainers/runc/libcontainer/utils"
2324
)
@@ -217,7 +218,11 @@ type runner struct {
217218
}
218219

219220
func (r *runner) run(config *specs.Process) (int, error) {
220-
var err error
221+
var (
222+
err error
223+
handlerCh chan *signalHandler
224+
status int
225+
)
221226
defer func() {
222227
if err != nil {
223228
r.destroy()
@@ -252,7 +257,15 @@ func (r *runner) run(config *specs.Process) (int, error) {
252257
// Setting up IO is a two stage process. We need to modify process to deal
253258
// with detaching containers, and then we get a tty after the container has
254259
// started.
255-
handlerCh := newSignalHandler(r.enableSubreaper)
260+
if r.enableSubreaper {
261+
// set us as the subreaper before registering the signal handler for the container
262+
if err := system.SetSubreaper(1); err != nil {
263+
logrus.Warn(err)
264+
}
265+
}
266+
if !detach {
267+
handlerCh = newSignalHandler()
268+
}
256269
tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket)
257270
if err != nil {
258271
return -1, err
@@ -297,15 +310,14 @@ func (r *runner) run(config *specs.Process) (int, error) {
297310
return -1, err
298311
}
299312
}
300-
handler := <-handlerCh
301-
status, err := handler.forward(process, tty, detach)
302-
if err != nil {
303-
r.terminate(process)
304-
}
305313
if detach {
306314
return 0, nil
307315
}
308-
if err == nil {
316+
// For non-detached container, we should forward signals to the container.
317+
handler := <-handlerCh
318+
if status, err = handler.forward(process, tty); err != nil {
319+
r.terminate(process)
320+
} else {
309321
r.destroy()
310322
}
311323
return status, err

0 commit comments

Comments
 (0)