diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c504ad5..8a2e78db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Next Release +* Fix bug which makes rails consoles to hang at exit when multiple of them are open (#647) + ## 2.1.1 * Avoid -I rubylibdir with default-gem bundler diff --git a/lib/spring/application.rb b/lib/spring/application.rb index 6d23d8fa..8a10fd11 100644 --- a/lib/spring/application.rb +++ b/lib/spring/application.rb @@ -12,6 +12,7 @@ def initialize(manager, original_env, spring_env = Env.new) @spring_env = spring_env @mutex = Mutex.new @waiting = Set.new + @clients = Set.new @preloaded = false @state = :initialized @interrupt = IO.pipe @@ -151,6 +152,8 @@ def serve(client) log "got client" manager.puts + @clients << client + _stdout, stderr, _stdin = streams = 3.times.map { client.recv_io } [STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) } @@ -167,6 +170,10 @@ def serve(client) end pid = fork { + # Make sure to close other clients otherwise their graceful termination + # will be impossible due to reference from this fork. + @clients.select { |c| c != client }.each(&:close) + Process.setsid IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") } trap("TERM", "DEFAULT")