Skip to content

Commit 6162d5f

Browse files
authored
Merge pull request #655 from casperisfine/boot-crash-stuck
Check that app preload was successful before sending a command.
2 parents 7ca9955 + 982273f commit 6162d5f

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

lib/spring/application.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,18 @@ def serve(client)
152152
_stdout, stderr, _stdin = streams = 3.times.map { client.recv_io }
153153
[STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) }
154154

155-
preload unless preloaded?
155+
if preloaded?
156+
client.puts(0) # preload success
157+
else
158+
begin
159+
preload
160+
client.puts(0) # preload success
161+
rescue Exception
162+
log "preload failed"
163+
client.puts(1) # preload failure
164+
raise
165+
end
166+
end
156167

157168
args, env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env")
158169
command = Spring.command(args.shift)

lib/spring/client/run.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,17 @@ def connect_to_application(client)
142142
end
143143

144144
def run_command(client, application)
145-
log "sending command"
146-
147145
application.send_io STDOUT
148146
application.send_io STDERR
149147
application.send_io STDIN
150148

149+
log "waiting for the application to be preloaded"
150+
preload_status = application.gets
151+
preload_status = preload_status.chomp if preload_status
152+
log "app preload status: #{preload_status}"
153+
exit 1 if preload_status == "1"
154+
155+
log "sending command"
151156
send_json application, "args" => args, "env" => ENV.to_hash
152157

153158
pid = server.gets

test/support/acceptance_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def without_gem(name)
9898
end
9999
end
100100

101+
test "crash on boot" do
102+
app.run app.spring_test_command, env: {
103+
"CRASH_ON_BOOT" => "1",
104+
# If the command is small enough, it might fit in the socket buffer and writing the command won't block.
105+
# So we send a big environment variable to better reproduce the problem.
106+
"FOO" => "bar" * 4_000,
107+
}
108+
end
109+
101110
test "help message when called without arguments" do
102111
assert_success "bin/spring", stdout: 'Usage: spring COMMAND [ARGS]'
103112
assert spring_env.server_running?

test/support/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def run(command, opts = {})
113113

114114
Bundler.with_clean_env do
115115
Process.spawn(
116-
env,
116+
env.merge(opts[:env] || {}),
117117
command.to_s,
118118
out: stdout.last,
119119
err: stderr.last,

test/support/application_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def generate_files
5454

5555
append_to_file(application.gemfile, "gem 'spring', '#{Spring::VERSION}'")
5656

57+
append_to_file(application.path("config/boot.rb"), "raise 'BOOM' if ENV['CRASH_ON_BOOT']")
58+
5759
rewrite_file(application.gemfile) do |c|
5860
c.sub!("https://rubygems.org", "http://rubygems.org")
5961
c.gsub!(/(gem '(byebug|web-console|sdoc|jbuilder)')/, "# \\1")

0 commit comments

Comments
 (0)