From c919bb80d4aa0436c6f7c645bad4d9f7a31eec22 Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Mon, 27 Nov 2017 12:40:09 +0000 Subject: [PATCH 1/4] Add failing tests --- lib/spring/test/acceptance_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/spring/test/acceptance_test.rb b/lib/spring/test/acceptance_test.rb index c3da1ce7..a6def602 100644 --- a/lib/spring/test/acceptance_test.rb +++ b/lib/spring/test/acceptance_test.rb @@ -194,6 +194,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" From 82d3bacf7d1977f3cd5e3b2a40590421f4220d20 Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Mon, 27 Nov 2017 12:41:01 +0000 Subject: [PATCH 2/4] Check if child process is alive --- lib/spring/application_manager.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/spring/application_manager.rb b/lib/spring/application_manager.rb index ca4671fb..f790e337 100644 --- a/lib/spring/application_manager.rb +++ b/lib/spring/application_manager.rb @@ -32,7 +32,11 @@ def restart end def alive? - @pid + return false if @pid.nil? + Process.getpgid(@pid) + true + rescue Errno::ESRCH + false end def with_child From f1300eb2c2a2d487ba250241738f044a858eea0c Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Mon, 27 Nov 2017 12:43:12 +0000 Subject: [PATCH 3/4] Check if the child app is still alive --- lib/spring/application_manager.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/spring/application_manager.rb b/lib/spring/application_manager.rb index f790e337..770bd47e 100644 --- a/lib/spring/application_manager.rb +++ b/lib/spring/application_manager.rb @@ -113,10 +113,20 @@ def start_child(preload = false) ) end - start_wait_thread(pid, child) if child.gets + wait_for_child_to_boot + start_wait_thread(pid, child) 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) From a44232f05bea1ad8594449dfa27eddc5fde34e50 Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Mon, 4 Dec 2017 09:19:19 +0000 Subject: [PATCH 4/4] Stop the server if we cannot start a child --- lib/spring/application_manager.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/spring/application_manager.rb b/lib/spring/application_manager.rb index 770bd47e..ae9795ac 100644 --- a/lib/spring/application_manager.rb +++ b/lib/spring/application_manager.rb @@ -115,6 +115,10 @@ def start_child(preload = false) 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