@@ -98,7 +98,7 @@ def running?
9898
9999 # Whether the container is stopping.
100100 def stopping?
101- @stopping
101+ @mutex . synchronize { @ stopping}
102102 end
103103
104104 # Sleep until a group event occurs or the specified duration elapses.
@@ -139,7 +139,7 @@ def wait
139139
140140 # Interrupt all children and enter the stopping state.
141141 def interrupt
142- @ stopping = true
142+ stopping!
143143 @group . interrupt!
144144 end
145145
@@ -155,15 +155,15 @@ def wait_until_ready
155155 return false if failed? && !lifecycle_running?
156156 return true if !running? && !lifecycle_running?
157157
158- @group . wait
158+ @group . wait ( 0.1 )
159159 end
160160 end
161161
162162 # Stop all children.
163163 def stop ( timeout = true )
164- return if @ stopping && !running?
164+ return if stopping? && !running?
165165
166- @ stopping = true
166+ stopping!
167167 @group . stop ( timeout )
168168 self . wait
169169 end
@@ -229,6 +229,10 @@ def lifecycle_running?
229229 @mutex . synchronize { @threads . any? ( &:alive? ) }
230230 end
231231
232+ def stopping!
233+ @mutex . synchronize { @stopping = true }
234+ end
235+
232236 def start_child ( name :, **options , &block )
233237 @child_type . call ( name : name , **options , &block )
234238 end
@@ -265,7 +269,7 @@ def manage_child(child, name:, key:, restart:, health_check_timeout:, startup_ti
265269 unregister_child ( child , status , key : key )
266270 notify_child_exit ( child , status , name : name , key : key )
267271
268- if restart && !@ stopping
272+ if restart && !stopping?
269273 @statistics . restart!
270274
271275 child = start_child ( name : name , **options , &block )
@@ -275,7 +279,7 @@ def manage_child(child, name:, key:, restart:, health_check_timeout:, startup_ti
275279 end
276280 end
277281 rescue => error
278- Console . error ( self , "Error during child lifecycle management!" , exception : error , stopping : @ stopping) if defined? ( Console )
282+ Console . error ( self , "Error during child lifecycle management!" , exception : error , stopping : stopping? ) if defined? ( Console )
279283 ensure
280284 if @group . children . include? ( child )
281285 begin
@@ -322,13 +326,15 @@ def monitor_child_with_timeouts(child, health_check_timeout:, startup_timeout:)
322326 end
323327
324328 def record_exit ( child , status , name :, key :)
329+ stopping = stopping?
330+
325331 if status &.success?
326- Console . debug ( self , "Child exited successfully." , status : status , stopping : @ stopping) if defined? ( Console )
327- elsif @ stopping
328- Console . debug ( self , "Child exited while stopping." , status : status , stopping : @ stopping) if defined? ( Console )
332+ Console . debug ( self , "Child exited successfully." , status : status , stopping : stopping ) if defined? ( Console )
333+ elsif stopping
334+ Console . debug ( self , "Child exited while stopping." , status : status , stopping : stopping ) if defined? ( Console )
329335 else
330336 @statistics . failure!
331- Console . error ( self , "Child exited with error!" , status : status , stopping : @ stopping) if defined? ( Console )
337+ Console . error ( self , "Child exited with error!" , status : status , stopping : stopping ) if defined? ( Console )
332338 end
333339 end
334340
0 commit comments