diff --git a/lib/spring/application_manager.rb b/lib/spring/application_manager.rb index 1a3325c8..8dfbb439 100644 --- a/lib/spring/application_manager.rb +++ b/lib/spring/application_manager.rb @@ -34,7 +34,11 @@ def restart end def alive? - @pid + return false if @pid.nil? + Process.getpgid(@pid) + true + rescue Errno::ESRCH + false end def with_child @@ -114,10 +118,24 @@ def start_child(preload = false) ) end - start_wait_thread(pid, child) if child.gets + wait_for_child_to_boot + start_wait_thread(pid, child) + rescue => e + log "error while starting child: #{e.message}" + abort("error while starting child: #{e.message}") + ensure child_socket.close end + def wait_for_child_to_boot + timeout = 1 + loop do + break if IO.select([child], nil, nil, timeout) + raise "child has exited unexpectedly" unless alive? + end + child.gets + end + def start_wait_thread(pid, child) Process.detach(pid) diff --git a/test/support/acceptance_test.rb b/test/support/acceptance_test.rb index d491996d..7d753024 100644 --- a/test/support/acceptance_test.rb +++ b/test/support/acceptance_test.rb @@ -248,6 +248,23 @@ def self.omg assert_success app.spring_test_command end + test "deleting the app root should kill the server" do + app.run app.spring_test_command + assert spring_env.server_running?, "The server should be running but it isn't" + FileUtils.rm_rf(app.root) + sleep 2 # wait for the watcher to notice and react + assert !spring_env.server_running?, "The server should not be running but it is" + end + + test "server restarts when the app root is deleted and recreated" do + assert_success app.spring_test_command + FileUtils.rm_rf(app.root) + sleep 1 # wait for the watcher to notice and react + + generator.copy_to(app.root) + assert_success app.spring_test_command + end + test "stop command kills server" do app.run app.spring_test_command assert spring_env.server_running?, "The server should be running but it isn't"