Skip to content

Commit c2c9fa3

Browse files
committed
Ensure the application wait thread is unaffected by broken pipes
In certain situations we could not get to the exit_if_finished call and the thread would just die silently. This would lead to the application process failing to exit at all, so it would have to be killed with a SIGKILL.
1 parent ad65fa1 commit c2c9fa3

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

lib/spring/application.rb

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,7 @@ def serve(client)
186186
log "forked #{pid}"
187187
manager.puts pid
188188

189-
# Wait in a separate thread so we can run multiple commands at once
190-
Thread.new {
191-
@mutex.synchronize { @waiting += 1 }
192-
193-
_, status = Process.wait2 pid
194-
log "#{pid} exited with #{status.exitstatus}"
195-
196-
streams.each(&:close)
197-
client.puts(status.exitstatus)
198-
client.close
199-
200-
@mutex.synchronize { @waiting -= 1 }
201-
exit_if_finished
202-
}
203-
189+
wait pid, streams, client
204190
rescue Exception => e
205191
log "exception: #{e}"
206192
manager.puts unless pid
@@ -296,6 +282,25 @@ def reset_streams
296282
STDIN.reopen("/dev/null")
297283
end
298284

285+
def wait(pid, streams, client)
286+
@mutex.synchronize { @waiting += 1 }
287+
288+
# Wait in a separate thread so we can run multiple commands at once
289+
Thread.new {
290+
begin
291+
_, status = Process.wait2 pid
292+
log "#{pid} exited with #{status.exitstatus}"
293+
294+
streams.each(&:close)
295+
client.puts(status.exitstatus)
296+
client.close
297+
ensure
298+
@mutex.synchronize { @waiting -= 1 }
299+
exit_if_finished
300+
end
301+
}
302+
end
303+
299304
private
300305

301306
def active_record_configured?

lib/spring/application_manager.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def run(client)
7676
end
7777

7878
def stop
79+
log "stopping"
7980
@state = :stopping
8081

8182
if pid

lib/spring/server.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def set_exit_hook
9292
end
9393

9494
def shutdown
95+
log "shutting down"
96+
9597
[env.socket_path, env.pidfile_path].each do |path|
9698
if path.exist?
9799
path.unlink rescue nil

0 commit comments

Comments
 (0)