@@ -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
219220func (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,15 @@ func (r *runner) run(config *specs.Process) (int, error) {
297309 return - 1 , err
298310 }
299311 }
312+ if detach {
313+ return 0 , nil
314+ }
315+ // For non-detached container, we should forward signals to the container.
300316 handler := <- handlerCh
301- status , err := handler .forward (process , tty , detach )
317+ status , err := handler .forward (process , tty )
302318 if err != nil {
303319 r .terminate (process )
304320 }
305- if detach {
306- return 0 , nil
307- }
308321 if err == nil {
309322 r .destroy ()
310323 }
0 commit comments