@@ -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 }()
@@ -247,11 +251,19 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
247251 }
248252 process .ExtraFiles = append (process .ExtraFiles , os .NewFile (uintptr (i ), "PreserveFD:" + strconv .Itoa (i )))
249253 }
250- detach := r .detach || (r .action == CT_ACT_CREATE )
251254 // Setting up IO is a two stage process. We need to modify process to deal
252255 // with detaching containers, and then we get a tty after the container has
253256 // started.
254- handlerCh := newSignalHandler (r .enableSubreaper )
257+ if r .enableSubreaper {
258+ // set us as the subreaper before registering the signal handler for the container
259+ if err := system .SetSubreaper (1 ); err != nil {
260+ logrus .Warn (err )
261+ }
262+ }
263+ var handlerCh chan * signalHandler
264+ if ! detach {
265+ handlerCh = newSignalHandler ()
266+ }
255267 tty , err := setupIO (process , r .container , config .Terminal , detach , r .consoleSocket )
256268 if err != nil {
257269 return - 1 , err
@@ -299,15 +311,12 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
299311 return - 1 , err
300312 }
301313 }
302- handler := <- handlerCh
303- status , err := handler .forward (process , tty , detach )
304314 if detach {
305315 return 0 , nil
306316 }
307- if err == nil {
308- r .destroy ()
309- }
310- return status , err
317+ // For non-detached container, we should forward signals to the container.
318+ handler := <- handlerCh
319+ return handler .forward (process , tty )
311320}
312321
313322func (r * runner ) destroy () {
0 commit comments