@@ -19,6 +19,7 @@ import (
1919 "github.com/opencontainers/runc/libcontainer"
2020 "github.com/opencontainers/runc/libcontainer/configs"
2121 "github.com/opencontainers/runc/libcontainer/specconv"
22+ "github.com/opencontainers/runc/libcontainer/system"
2223 "github.com/opencontainers/runc/libcontainer/system/kernelversion"
2324 "github.com/opencontainers/runc/libcontainer/utils"
2425)
@@ -218,8 +219,11 @@ type runner struct {
218219}
219220
220221func (r * runner ) run (config * specs.Process ) (_ int , retErr error ) {
222+ detach := r .detach || (r .action == CT_ACT_CREATE )
221223 defer func () {
222- if retErr != nil {
224+ // For a non-detached container, or we get an error, we
225+ // should destroy the container.
226+ if ! detach || retErr != nil {
223227 r .destroy ()
224228 }
225229 }()
@@ -248,11 +252,19 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
248252 }
249253 process .ExtraFiles = append (process .ExtraFiles , os .NewFile (uintptr (i ), "PreserveFD:" + strconv .Itoa (i )))
250254 }
251- detach := r .detach || (r .action == CT_ACT_CREATE )
252255 // Setting up IO is a two stage process. We need to modify process to deal
253256 // with detaching containers, and then we get a tty after the container has
254257 // started.
255- handlerCh := newSignalHandler (r .enableSubreaper )
258+ if r .enableSubreaper {
259+ // set us as the subreaper before registering the signal handler for the container
260+ if err := system .SetSubreaper (1 ); err != nil {
261+ logrus .Warn (err )
262+ }
263+ }
264+ var handlerCh chan * signalHandler
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
@@ -300,15 +312,12 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
300312 return - 1 , err
301313 }
302314 }
303- handler := <- handlerCh
304- status , err := handler .forward (process , tty , detach )
305315 if detach {
306316 return 0 , nil
307317 }
308- if err == nil {
309- r .destroy ()
310- }
311- return status , err
318+ // For non-detached container, we should forward signals to the container.
319+ handler := <- handlerCh
320+ return handler .forward (process , tty )
312321}
313322
314323func (r * runner ) destroy () {
0 commit comments