@@ -383,6 +383,12 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
383383
384384 switch status .Status {
385385 case containerd .Created , containerd .Stopped :
386+ // Cleanup the IO after a successful Stop
387+ if io := task .IO (); io != nil {
388+ if cerr := io .Close (); cerr != nil {
389+ log .G (ctx ).Warnf ("failed to close IO for container %s: %v" , container .ID (), cerr )
390+ }
391+ }
386392 return nil
387393 case containerd .Paused , containerd .Pausing :
388394 paused = true
@@ -395,6 +401,13 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
395401 return err
396402 }
397403
404+ // signal will be sent once resume is finished
405+ if paused {
406+ if err := task .Resume (ctx ); err != nil {
407+ log .G (ctx ).Errorf ("cannot unpause container %s: %s" , container .ID (), err )
408+ return err
409+ }
410+ }
398411 if * timeout > 0 {
399412 sig , err := getSignal (signalValue , l )
400413 if err != nil {
@@ -405,20 +418,10 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
405418 return err
406419 }
407420
408- // signal will be sent once resume is finished
409- if paused {
410- if err := task .Resume (ctx ); err != nil {
411- log .G (ctx ).Warnf ("Cannot unpause container %s: %s" , container .ID (), err )
412- } else {
413- // no need to do it again when send sigkill signal
414- paused = false
415- }
416- }
417-
418421 sigtermCtx , sigtermCtxCancel := context .WithTimeout (ctx , * timeout )
419422 defer sigtermCtxCancel ()
420423
421- err = waitContainerStop (sigtermCtx , exitCh , container .ID ())
424+ err = waitContainerStop (sigtermCtx , task , exitCh , container .ID ())
422425 if err == nil {
423426 return nil
424427 }
@@ -437,13 +440,7 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
437440 return err
438441 }
439442
440- // signal will be sent once resume is finished
441- if paused {
442- if err := task .Resume (ctx ); err != nil {
443- log .G (ctx ).Warnf ("Cannot unpause container %s: %s" , container .ID (), err )
444- }
445- }
446- return waitContainerStop (ctx , exitCh , container .ID ())
443+ return waitContainerStop (ctx , task , exitCh , container .ID ())
447444}
448445
449446func getSignal (signalValue string , containerLabels map [string ]string ) (syscall.Signal , error ) {
@@ -458,14 +455,20 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
458455 return signal .ParseSignal ("SIGTERM" )
459456}
460457
461- func waitContainerStop (ctx context.Context , exitCh <- chan containerd.ExitStatus , id string ) error {
458+ func waitContainerStop (ctx context.Context , task containerd. Task , exitCh <- chan containerd.ExitStatus , id string ) error {
462459 select {
463460 case <- ctx .Done ():
464461 if err := ctx .Err (); err != nil {
465462 return fmt .Errorf ("wait container %v: %w" , id , err )
466463 }
467464 return nil
468465 case status := <- exitCh :
466+ // Cleanup the IO after a successful Stop
467+ if io := task .IO (); io != nil {
468+ if cerr := io .Close (); cerr != nil {
469+ log .G (ctx ).Warnf ("failed to close IO for container %s: %v" , id , cerr )
470+ }
471+ }
469472 return status .Error ()
470473 }
471474}
0 commit comments