@@ -385,6 +385,12 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
385
385
386
386
switch status .Status {
387
387
case containerd .Created , containerd .Stopped :
388
+ // Cleanup the IO after a successful Stop
389
+ if io := task .IO (); io != nil {
390
+ if cerr := io .Close (); cerr != nil {
391
+ log .G (ctx ).Warnf ("failed to close IO for container %s: %v" , container .ID (), cerr )
392
+ }
393
+ }
388
394
return nil
389
395
case containerd .Paused , containerd .Pausing :
390
396
paused = true
@@ -397,6 +403,13 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
397
403
return err
398
404
}
399
405
406
+ // signal will be sent once resume is finished
407
+ if paused {
408
+ if err := task .Resume (ctx ); err != nil {
409
+ log .G (ctx ).Errorf ("Cannot unpause container %s: %s" , container .ID (), err )
410
+ return err
411
+ }
412
+ }
400
413
if * timeout > 0 {
401
414
sig , err := getSignal (signalValue , l )
402
415
if err != nil {
@@ -407,20 +420,10 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
407
420
return err
408
421
}
409
422
410
- // signal will be sent once resume is finished
411
- if paused {
412
- if err := task .Resume (ctx ); err != nil {
413
- log .G (ctx ).Warnf ("Cannot unpause container %s: %s" , container .ID (), err )
414
- } else {
415
- // no need to do it again when send sigkill signal
416
- paused = false
417
- }
418
- }
419
-
420
423
sigtermCtx , sigtermCtxCancel := context .WithTimeout (ctx , * timeout )
421
424
defer sigtermCtxCancel ()
422
425
423
- err = waitContainerStop (sigtermCtx , exitCh , container .ID ())
426
+ err = waitContainerStop (sigtermCtx , task , exitCh , container .ID ())
424
427
if err == nil {
425
428
return nil
426
429
}
@@ -439,13 +442,7 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
439
442
return err
440
443
}
441
444
442
- // signal will be sent once resume is finished
443
- if paused {
444
- if err := task .Resume (ctx ); err != nil {
445
- log .G (ctx ).Warnf ("Cannot unpause container %s: %s" , container .ID (), err )
446
- }
447
- }
448
- return waitContainerStop (ctx , exitCh , container .ID ())
445
+ return waitContainerStop (ctx , task , exitCh , container .ID ())
449
446
}
450
447
451
448
func getSignal (signalValue string , containerLabels map [string ]string ) (syscall.Signal , error ) {
@@ -460,7 +457,7 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
460
457
return signal .ParseSignal ("SIGTERM" )
461
458
}
462
459
463
- func waitContainerStop (ctx context.Context , exitCh <- chan containerd.ExitStatus , id string ) error {
460
+ func waitContainerStop (ctx context.Context , task containerd. Task , exitCh <- chan containerd.ExitStatus , id string ) error {
464
461
select {
465
462
case <- ctx .Done ():
466
463
if err := ctx .Err (); err != nil {
0 commit comments