@@ -68,6 +68,7 @@ type Container struct {
6868 stopLock sync.Mutex
6969 stopTimeoutChan chan int64
7070 stopWatchers []chan struct {}
71+ stopKillLoopBegun bool
7172 pidns nsmgr.Namespace
7273 restore bool
7374 restoreArchivePath string
@@ -145,22 +146,23 @@ func NewContainer(id, name, bundlePath, logPath string, labels, crioAnnotations,
145146 },
146147 ImageRef : externalImageRef ,
147148 },
148- name : name ,
149- bundlePath : bundlePath ,
150- logPath : logPath ,
151- terminal : terminal ,
152- stdin : stdin ,
153- stdinOnce : stdinOnce ,
154- runtimeHandler : runtimeHandler ,
155- crioAnnotations : crioAnnotations ,
156- imageName : imageName ,
157- imageID : imageID ,
158- dir : dir ,
159- state : state ,
160- stopSignal : stopSignal ,
161- stopTimeoutChan : make (chan int64 , 10 ),
162- stopWatchers : []chan struct {}{},
163- execPIDs : map [int ]bool {},
149+ name : name ,
150+ bundlePath : bundlePath ,
151+ logPath : logPath ,
152+ terminal : terminal ,
153+ stdin : stdin ,
154+ stdinOnce : stdinOnce ,
155+ runtimeHandler : runtimeHandler ,
156+ crioAnnotations : crioAnnotations ,
157+ imageName : imageName ,
158+ imageID : imageID ,
159+ dir : dir ,
160+ state : state ,
161+ stopSignal : stopSignal ,
162+ stopTimeoutChan : make (chan int64 , 10 ),
163+ stopWatchers : []chan struct {}{},
164+ stopKillLoopBegun : false ,
165+ execPIDs : map [int ]bool {},
164166 }
165167 return c , nil
166168}
@@ -600,14 +602,30 @@ func (c *Container) SetAsStopping() (setToStopping bool) {
600602 return false
601603}
602604
605+ // SetStopKillLoopBegun sets the stopKillLoopBegun flag to true.
606+ func (c * Container ) SetStopKillLoopBegun () {
607+ c .stopLock .Lock ()
608+ defer c .stopLock .Unlock ()
609+ c .stopKillLoopBegun = true
610+ }
611+
603612func (c * Container ) WaitOnStopTimeout (ctx context.Context , timeout int64 ) {
604613 c .stopLock .Lock ()
605614 if ! c .stopping {
606615 c .stopLock .Unlock ()
607616 return
608617 }
609618
610- c .stopTimeoutChan <- timeout
619+ // Don't use the stopTimeoutChan when the container is in kill loop
620+ // because values in the channel are no longer consumed.
621+ if ! c .stopKillLoopBegun {
622+ // Use select and default not to block when the stopTimeoutChan is full.
623+ // The channel is very unlikely to be full, but it could happen in theory.
624+ select {
625+ case c .stopTimeoutChan <- timeout :
626+ default :
627+ }
628+ }
611629
612630 watcher := make (chan struct {}, 1 )
613631 c .stopWatchers = append (c .stopWatchers , watcher )
0 commit comments