@@ -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
219220func (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
@@ -292,20 +305,19 @@ func (r *runner) run(config *specs.Process) (int, error) {
292305 }
293306 }
294307 if r .notifySocket != nil {
295- if err : = r .notifySocket .forward (process , detach ); err != nil {
308+ if err = r .notifySocket .forward (process , detach ); err != nil {
296309 r .terminate (process )
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