@@ -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,11 @@ 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+ // For a non-detached container, or we get an error, we
224+ // should destroy the container.
225+ if ! detach || retErr != nil {
222226 r .destroy ()
223227 }
224228 }()
@@ -255,11 +259,19 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
255259 }
256260 process .ExtraFiles = append (process .ExtraFiles , os .NewFile (uintptr (i ), "PreserveFD:" + strconv .Itoa (i )))
257261 }
258- detach := r .detach || (r .action == CT_ACT_CREATE )
259262 // Setting up IO is a two stage process. We need to modify process to deal
260263 // with detaching containers, and then we get a tty after the container has
261264 // started.
262- handlerCh := newSignalHandler (r .enableSubreaper )
265+ if r .enableSubreaper {
266+ // set us as the subreaper before registering the signal handler for the container
267+ if err := system .SetSubreaper (1 ); err != nil {
268+ logrus .Warn (err )
269+ }
270+ }
271+ var handlerCh chan * signalHandler
272+ if ! detach {
273+ handlerCh = newSignalHandler ()
274+ }
263275 tty , err := setupIO (process , r .container , config .Terminal , detach , r .consoleSocket )
264276 if err != nil {
265277 return - 1 , err
@@ -302,15 +314,12 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
302314 return - 1 , err
303315 }
304316 }
305- handler := <- handlerCh
306- status , err := handler .forward (process , tty , detach )
307317 if detach {
308318 return 0 , nil
309319 }
310- if err == nil {
311- r .destroy ()
312- }
313- return status , err
320+ // For non-detached container, we should forward signals to the container.
321+ handler := <- handlerCh
322+ return handler .forward (process , tty )
314323}
315324
316325func (r * runner ) destroy () {
0 commit comments