@@ -397,30 +397,25 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
397
397
return err
398
398
}
399
399
400
+ // signal will be sent once resume is finished
401
+ if paused {
402
+ if err := task .Resume (ctx ); err != nil {
403
+ log .G (ctx ).Errorf ("Cannot unpause container %s: %s" , container .ID (), err )
404
+ return err
405
+ }
406
+ }
400
407
if * timeout > 0 {
401
408
sig , err := getSignal (signalValue , l )
402
409
if err != nil {
403
410
return err
404
411
}
405
-
406
412
if err := task .Kill (ctx , sig ); err != nil {
407
413
return err
408
414
}
409
-
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
415
sigtermCtx , sigtermCtxCancel := context .WithTimeout (ctx , * timeout )
421
416
defer sigtermCtxCancel ()
422
417
423
- err = waitContainerStop (sigtermCtx , exitCh , container .ID ())
418
+ err = waitContainerStop (sigtermCtx , task , exitCh , container .ID ())
424
419
if err == nil {
425
420
return nil
426
421
}
@@ -434,18 +429,10 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
434
429
if err != nil {
435
430
return err
436
431
}
437
-
438
432
if err := task .Kill (ctx , sig ); err != nil {
439
433
return err
440
434
}
441
-
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 ())
435
+ return waitContainerStop (ctx , task , exitCh , container .ID ())
449
436
}
450
437
451
438
func getSignal (signalValue string , containerLabels map [string ]string ) (syscall.Signal , error ) {
@@ -460,14 +447,23 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
460
447
return signal .ParseSignal ("SIGTERM" )
461
448
}
462
449
463
- func waitContainerStop (ctx context.Context , exitCh <- chan containerd.ExitStatus , id string ) error {
450
+ func waitContainerStop (ctx context.Context , task containerd. Task , exitCh <- chan containerd.ExitStatus , id string ) error {
464
451
select {
465
452
case <- ctx .Done ():
466
453
if err := ctx .Err (); err != nil {
467
454
return fmt .Errorf ("wait container %v: %w" , id , err )
468
455
}
469
456
return nil
470
457
case status := <- exitCh :
458
+ // Cleanup the IO after a successful Stop
459
+ io := task .IO ()
460
+ if task .IO () != nil {
461
+ defer func () {
462
+ if cerr := io .Close (); cerr != nil {
463
+ log .G (ctx ).Warnf ("failed to close IO for container %s: %v" , id , cerr )
464
+ }
465
+ }()
466
+ }
471
467
return status .Error ()
472
468
}
473
469
}
0 commit comments