Skip to content

Commit 56e3f98

Browse files
yprestojonleighton
authored andcommitted
Fix STDOUT/ERR kept open after preload failure (#423)
* Fix STDOUT/ERR kept open after preload failure * Add test for piping with boot-level error
1 parent 3399e5a commit 56e3f98

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/spring/application.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ def serve(client)
192192
}
193193

194194
disconnect_database
195-
reset_streams
196195

197196
log "forked #{pid}"
198197
manager.puts pid
@@ -209,6 +208,11 @@ def serve(client)
209208

210209
client.puts(1) if pid
211210
client.close
211+
ensure
212+
# Redirect STDOUT and STDERR to prevent from keeping the original FDs
213+
# (i.e. to prevent `spring rake -T | grep db` from hanging forever),
214+
# even when exception is raised before forking (i.e. preloading).
215+
reset_streams
212216
end
213217

214218
def terminate
@@ -292,8 +296,11 @@ def with_pty
292296
PTY.open do |master, slave|
293297
[STDOUT, STDERR, STDIN].each { |s| s.reopen slave }
294298
Thread.new { master.read }
295-
yield
296-
reset_streams
299+
begin
300+
yield
301+
ensure
302+
reset_streams
303+
end
297304
end
298305
end
299306

lib/spring/test/acceptance_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ def exec_name
415415
assert_failure "bin/rake -T", stderr: "unable to find your config/application.rb"
416416
end
417417

418+
test "piping with boot-level error" do
419+
config = app.application_config.read
420+
File.write(app.application_config, "#{config}\nomg")
421+
assert_success "bin/rake -T | cat"
422+
end
423+
418424
test "piping" do
419425
assert_success "bin/rake -T | grep db", stdout: "rake db:migrate"
420426
end

0 commit comments

Comments
 (0)