Skip to content

Fix hanging consoles: Close other client sockets in spring application forks #647

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next Release

* Fix bug which makes rails consoles to hang at exit when multiple of them are open (#647)

## 2.1.1

* Avoid -I rubylibdir with default-gem bundler
Expand Down
7 changes: 7 additions & 0 deletions lib/spring/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(manager, original_env, spring_env = Env.new)
@spring_env = spring_env
@mutex = Mutex.new
@waiting = Set.new
@clients = Set.new
@preloaded = false
@state = :initialized
@interrupt = IO.pipe
Expand Down Expand Up @@ -151,6 +152,8 @@ def serve(client)
log "got client"
manager.puts

@clients << client

_stdout, stderr, _stdin = streams = 3.times.map { client.recv_io }
[STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) }

Expand All @@ -167,6 +170,10 @@ def serve(client)
end

pid = fork {
# Make sure to close other clients otherwise their graceful termination
# will be impossible due to reference from this fork.
@clients.select { |c| c != client }.each(&:close)

Process.setsid
IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") }
trap("TERM", "DEFAULT")
Expand Down