@@ -69,6 +69,7 @@ type Container struct {
6969 stopLock sync.Mutex
7070 stopTimeoutChan chan int64
7171 stopWatchers []chan struct {}
72+ stopKillLoopBegun bool
7273 pidns nsmgr.Namespace
7374 restore bool
7475 restoreArchivePath string
@@ -155,22 +156,23 @@ func NewContainer(id, name, bundlePath, logPath string, labels, crioAnnotations,
155156 ImageRef : externalImageRef ,
156157 ImageId : imageIDString ,
157158 },
158- name : name ,
159- bundlePath : bundlePath ,
160- logPath : logPath ,
161- terminal : terminal ,
162- stdin : stdin ,
163- stdinOnce : stdinOnce ,
164- runtimeHandler : runtimeHandler ,
165- crioAnnotations : crioAnnotations ,
166- imageName : imageName ,
167- imageID : imageID ,
168- dir : dir ,
169- state : state ,
170- stopSignal : stopSignal ,
171- stopTimeoutChan : make (chan int64 , 10 ),
172- stopWatchers : []chan struct {}{},
173- execPIDs : map [int ]bool {},
159+ name : name ,
160+ bundlePath : bundlePath ,
161+ logPath : logPath ,
162+ terminal : terminal ,
163+ stdin : stdin ,
164+ stdinOnce : stdinOnce ,
165+ runtimeHandler : runtimeHandler ,
166+ crioAnnotations : crioAnnotations ,
167+ imageName : imageName ,
168+ imageID : imageID ,
169+ dir : dir ,
170+ state : state ,
171+ stopSignal : stopSignal ,
172+ stopTimeoutChan : make (chan int64 , 10 ),
173+ stopWatchers : []chan struct {}{},
174+ stopKillLoopBegun : false ,
175+ execPIDs : map [int ]bool {},
174176 }
175177 return c , nil
176178}
@@ -611,14 +613,30 @@ func (c *Container) SetAsStopping() (setToStopping bool) {
611613 return false
612614}
613615
616+ // SetStopKillLoopBegun sets the stopKillLoopBegun flag to true.
617+ func (c * Container ) SetStopKillLoopBegun () {
618+ c .stopLock .Lock ()
619+ defer c .stopLock .Unlock ()
620+ c .stopKillLoopBegun = true
621+ }
622+
614623func (c * Container ) WaitOnStopTimeout (ctx context.Context , timeout int64 ) {
615624 c .stopLock .Lock ()
616625 if ! c .stopping {
617626 c .stopLock .Unlock ()
618627 return
619628 }
620629
621- c .stopTimeoutChan <- timeout
630+ // Don't use the stopTimeoutChan when the container is in kill loop
631+ // because values in the channel are no longer consumed.
632+ if ! c .stopKillLoopBegun {
633+ // Use select and default not to block when the stopTimeoutChan is full.
634+ // The channel is very unlikely to be full, but it could happen in theory.
635+ select {
636+ case c .stopTimeoutChan <- timeout :
637+ default :
638+ }
639+ }
622640
623641 watcher := make (chan struct {}, 1 )
624642 c .stopWatchers = append (c .stopWatchers , watcher )
0 commit comments