@@ -18,6 +18,7 @@ import (
18
18
"github.com/opencontainers/runc/libcontainer"
19
19
"github.com/opencontainers/runc/libcontainer/configs"
20
20
"github.com/opencontainers/runc/libcontainer/specconv"
21
+ "github.com/opencontainers/runc/libcontainer/system"
21
22
"github.com/opencontainers/runc/libcontainer/system/kernelversion"
22
23
"github.com/opencontainers/runc/libcontainer/utils"
23
24
)
@@ -217,8 +218,11 @@ type runner struct {
217
218
}
218
219
219
220
func (r * runner ) run (config * specs.Process ) (_ int , retErr error ) {
221
+ detach := r .detach || (r .action == CT_ACT_CREATE )
220
222
defer func () {
221
- if retErr != nil {
223
+ // For a non-detached container, or we get an error, we
224
+ // should destory the container.
225
+ if ! detach || retErr != nil {
222
226
r .destroy ()
223
227
}
224
228
}()
@@ -255,11 +259,19 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
255
259
}
256
260
process .ExtraFiles = append (process .ExtraFiles , os .NewFile (uintptr (i ), "PreserveFD:" + strconv .Itoa (i )))
257
261
}
258
- detach := r .detach || (r .action == CT_ACT_CREATE )
259
262
// Setting up IO is a two stage process. We need to modify process to deal
260
263
// with detaching containers, and then we get a tty after the container has
261
264
// 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
+ }
263
275
tty , err := setupIO (process , r .container , config .Terminal , detach , r .consoleSocket )
264
276
if err != nil {
265
277
return - 1 , err
@@ -302,15 +314,12 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
302
314
return - 1 , err
303
315
}
304
316
}
305
- handler := <- handlerCh
306
- status , err := handler .forward (process , tty , detach )
307
317
if detach {
308
318
return 0 , nil
309
319
}
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 )
314
323
}
315
324
316
325
func (r * runner ) destroy () {
0 commit comments