@@ -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,8 +218,9 @@ type runner struct {
217218}
218219
219220func (r * runner ) run (config * specs.Process ) (_ int , retErr error ) {
221+ detach := r .detach || (r .action == CT_ACT_CREATE )
220222 defer func () {
221- if retErr != nil {
223+ if ! detach || retErr != nil {
222224 r .destroy ()
223225 }
224226 }()
@@ -247,11 +249,19 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
247249 }
248250 process .ExtraFiles = append (process .ExtraFiles , os .NewFile (uintptr (i ), "PreserveFD:" + strconv .Itoa (i )))
249251 }
250- detach := r .detach || (r .action == CT_ACT_CREATE )
251252 // Setting up IO is a two stage process. We need to modify process to deal
252253 // with detaching containers, and then we get a tty after the container has
253254 // started.
254- handlerCh := newSignalHandler (r .enableSubreaper )
255+ if r .enableSubreaper {
256+ // set us as the subreaper before registering the signal handler for the container
257+ if err := system .SetSubreaper (1 ); err != nil {
258+ logrus .Warn (err )
259+ }
260+ }
261+ var handlerCh chan * signalHandler
262+ if ! detach {
263+ handlerCh = newSignalHandler ()
264+ }
255265 tty , err := setupIO (process , r .container , config .Terminal , detach , r .consoleSocket )
256266 if err != nil {
257267 return - 1 , err
@@ -296,16 +306,14 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
296306 return - 1 , err
297307 }
298308 }
299- handler := <- handlerCh
300- status , err := handler .forward (process , tty , detach )
301- if err != nil {
302- r .terminate (process )
303- }
304309 if detach {
305310 return 0 , nil
306311 }
307- if err == nil {
308- r .destroy ()
312+ // For non-detached container, we should forward signals to the container.
313+ var status int
314+ handler := <- handlerCh
315+ if status , err = handler .forward (process , tty ); err != nil {
316+ r .terminate (process )
309317 }
310318 return status , err
311319}
0 commit comments