Skip to content

Commit bec1b8e

Browse files
committed
Time out after 10 seconds if starting the spring server doesn't work
Maybe related to #480, #479
1 parent 487a326 commit bec1b8e

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Specify absolute path to spring binfile when starting the server
44
(#478)
5+
* Time out after 10 seconds if starting the spring server doesn't work
6+
(maybe related to #480, #479)
57

68
## 1.7.0
79

lib/spring/client/run.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Spring
66
module Client
77
class Run < Command
88
FORWARDED_SIGNALS = %w(INT QUIT USR1 USR2 INFO WINCH) & Signal.list.keys
9-
TIMEOUT = 1
9+
CONNECT_TIMEOUT = 1
10+
BOOT_TIMEOUT = 10
1011

1112
attr_reader :server
1213

@@ -69,11 +70,21 @@ def run
6970

7071
def boot_server
7172
env.socket_path.unlink if env.socket_path.exist?
72-
pid = Process.spawn(gem_env, env.server_command, out: File::NULL)
73+
74+
pid = Process.spawn(gem_env, env.server_command, out: File::NULL)
75+
timeout = Time.now + BOOT_TIMEOUT
7376

7477
until env.socket_path.exist?
7578
_, status = Process.waitpid2(pid, Process::WNOHANG)
76-
exit status.exitstatus if status
79+
80+
if status
81+
exit status.exitstatus
82+
elsif Time.now > timeout
83+
$stderr.puts "Starting Spring server with `#{env.server_command}` " \
84+
"timed out after #{BOOT_TIMEOUT} seconds"
85+
exit 1
86+
end
87+
7788
sleep 0.1
7889
end
7990
end
@@ -111,7 +122,7 @@ def connect_to_application(client)
111122
server.send_io client
112123
send_json server, "args" => args, "default_rails_env" => default_rails_env
113124

114-
if IO.select([server], [], [], TIMEOUT)
125+
if IO.select([server], [], [], CONNECT_TIMEOUT)
115126
server.gets or raise CommandNotFound
116127
else
117128
raise "Error connecting to Spring server"

lib/spring/test/acceptance_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def exec_name
404404
end
405405

406406
test "can define client tasks" do
407-
File.write("#{app.spring_config.sub('.rb', '_client.rb')}", <<-RUBY)
407+
File.write("#{app.spring_client_config}", <<-RUBY)
408408
Spring::Client::COMMANDS["foo"] = lambda { |args| puts "bar -- \#{args.inspect}" }
409409
RUBY
410410
assert_success "bin/spring foo --baz", stdout: "bar -- [\"foo\", \"--baz\"]\n"
@@ -525,6 +525,15 @@ def exec_name
525525
assert_success app.spring_test_command
526526
end
527527
end
528+
529+
test "server boot timeout" do
530+
app.env["SPRING_SERVER_COMMAND"] = "sleep 1"
531+
app.spring_client_config.write %(
532+
Spring::Client::Run.const_set(:BOOT_TIMEOUT, 0.1)
533+
)
534+
535+
assert_failure "bin/rails runner ''", stderr: "timed out"
536+
end
528537
end
529538
end
530539
end

lib/spring/test/application.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def spring_config
8888
path "config/spring.rb"
8989
end
9090

91+
def spring_client_config
92+
path "config/spring_client.rb"
93+
end
94+
9195
def run(command, opts = {})
9296
start_time = Time.now
9397

0 commit comments

Comments
 (0)