Skip to content

Commit 790b750

Browse files
committed
Ensure application processes exit immediately on second SIGTERM
If the termination logic is misbehaving in some way, this ensures that we are not forced to do a SIGKILL.
1 parent c2c9fa3 commit 790b750

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/spring/application.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(manager, original_env)
1111
@original_env = original_env
1212
@spring_env = Env.new
1313
@mutex = Mutex.new
14-
@waiting = 0
14+
@waiting = Set.new
1515
@preloaded = false
1616
@state = :initialized
1717
@interrupt = IO.pipe
@@ -201,7 +201,14 @@ def serve(client)
201201
end
202202

203203
def terminate
204-
state! :terminating
204+
if exiting?
205+
# Ensure that we do not ignore subsequent termination attempts
206+
log "forced exit"
207+
@waiting.each { |pid| Process.kill("TERM", pid) }
208+
Kernel.exit
209+
else
210+
state! :terminating
211+
end
205212
end
206213

207214
def exit
@@ -213,7 +220,7 @@ def exit
213220

214221
def exit_if_finished
215222
@mutex.synchronize {
216-
Kernel.exit if exiting? && @waiting == 0
223+
Kernel.exit if exiting? && @waiting.empty?
217224
}
218225
end
219226

@@ -283,7 +290,7 @@ def reset_streams
283290
end
284291

285292
def wait(pid, streams, client)
286-
@mutex.synchronize { @waiting += 1 }
293+
@mutex.synchronize { @waiting << pid }
287294

288295
# Wait in a separate thread so we can run multiple commands at once
289296
Thread.new {
@@ -295,7 +302,7 @@ def wait(pid, streams, client)
295302
client.puts(status.exitstatus)
296303
client.close
297304
ensure
298-
@mutex.synchronize { @waiting -= 1 }
305+
@mutex.synchronize { @waiting.delete pid }
299306
exit_if_finished
300307
end
301308
}

0 commit comments

Comments
 (0)