Skip to content

Commit 7dfb066

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 8fa3113 commit 7dfb066

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
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: 18 additions & 6 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,10 @@ 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+
)
221225
defer func() {
222226
if err != nil {
223227
r.destroy()
@@ -252,7 +256,15 @@ func (r *runner) run(config *specs.Process) (int, error) {
252256
// Setting up IO is a two stage process. We need to modify process to deal
253257
// with detaching containers, and then we get a tty after the container has
254258
// started.
255-
handlerCh := newSignalHandler(r.enableSubreaper)
259+
if r.enableSubreaper {
260+
// set us as the subreaper before registering the signal handler for the container
261+
if err := system.SetSubreaper(1); err != nil {
262+
logrus.Warn(err)
263+
}
264+
}
265+
if !detach {
266+
handlerCh = newSignalHandler()
267+
}
256268
tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket)
257269
if err != nil {
258270
return -1, err
@@ -297,14 +309,14 @@ func (r *runner) run(config *specs.Process) (int, error) {
297309
return -1, err
298310
}
299311
}
312+
if detach {
313+
return 0, nil
314+
}
300315
handler := <-handlerCh
301-
status, err := handler.forward(process, tty, detach)
316+
status, err := handler.forward(process, tty)
302317
if err != nil {
303318
r.terminate(process)
304319
}
305-
if detach {
306-
return 0, nil
307-
}
308320
if err == nil {
309321
r.destroy()
310322
}

0 commit comments

Comments
 (0)