Skip to content

Check that app preload was successful before sending a command. #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/spring/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,18 @@ def serve(client)
_stdout, stderr, _stdin = streams = 3.times.map { client.recv_io }
[STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) }

preload unless preloaded?
if preloaded?
client.puts(0) # preload success
else
begin
preload
client.puts(0) # preload success
rescue Exception
log "preload failed"
client.puts(1) # preload failure
raise
end
end

args, env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env")
command = Spring.command(args.shift)
Expand Down
9 changes: 7 additions & 2 deletions lib/spring/client/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,17 @@ def connect_to_application(client)
end

def run_command(client, application)
log "sending command"

application.send_io STDOUT
application.send_io STDERR
application.send_io STDIN

log "waiting for the application to be preloaded"
preload_status = application.gets
preload_status = preload_status.chomp if preload_status
log "app preload status: #{preload_status}"
exit 1 if preload_status == "1"

log "sending command"
send_json application, "args" => args, "env" => ENV.to_hash

pid = server.gets
Expand Down
9 changes: 9 additions & 0 deletions test/support/acceptance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def without_gem(name)
end
end

test "crash on boot" do
app.run app.spring_test_command, env: {
"CRASH_ON_BOOT" => "1",
# If the command is small enough, it might fit in the socket buffer and writing the command won't block.
# So we send a big environment variable to better reproduce the problem.
"FOO" => "bar" * 4_000,
}
end

test "help message when called without arguments" do
assert_success "bin/spring", stdout: 'Usage: spring COMMAND [ARGS]'
assert spring_env.server_running?
Expand Down
2 changes: 1 addition & 1 deletion test/support/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def run(command, opts = {})

Bundler.with_clean_env do
Process.spawn(
env,
env.merge(opts[:env] || {}),
command.to_s,
out: stdout.last,
err: stderr.last,
Expand Down
2 changes: 2 additions & 0 deletions test/support/application_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def generate_files

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

append_to_file(application.path("config/boot.rb"), "raise 'BOOM' if ENV['CRASH_ON_BOOT']")

rewrite_file(application.gemfile) do |c|
c.sub!("https://rubygems.org", "http://rubygems.org")
c.gsub!(/(gem '(byebug|web-console|sdoc|jbuilder)')/, "# \\1")
Expand Down