Skip to content

Set process group ID in server to terminate after parent #276

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
Feb 29, 2024
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
16 changes: 14 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initialize
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
# set its own session ID
begin
Process.setpgrp
Process.setsid
rescue Errno::EPERM
# If we can't set the session ID, continue
Expand All @@ -52,6 +53,16 @@ def initialize
warn("Ruby LSP Rails booting server")
read_response
warn("Finished booting Ruby LSP Rails server")

unless ENV["RAILS_ENV"] == "test"
at_exit do
if @wait_thread.alive?
warn("Ruby LSP Rails is force killing the server")
sleep(0.5) # give the server a bit of time if we already issued a shutdown notification
Process.kill(T.must(Signal.list["TERM"]), @wait_thread.pid)
end
end
end
rescue Errno::EPIPE, IncompleteMessageError
raise InitializationError, @stderr.read
end
Expand All @@ -66,8 +77,9 @@ def model(name)

sig { void }
def shutdown
send_notification("shutdown")
Thread.pass while @wait_thread.alive?
warn("Ruby LSP Rails shutting down server")
send_message("shutdown")
sleep(0.5) # give the server a bit of time to shutdown
[@stdin, @stdout, @stderr].each(&:close)
end

Expand Down
4 changes: 2 additions & 2 deletions test/ruby_lsp_rails/runner_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ module RubyLsp
module Rails
class RunnerClientTest < ActiveSupport::TestCase
setup do
capture_io do
capture_subprocess_io do
@client = T.let(RunnerClient.new, RunnerClient)
end
end

teardown do
@client.shutdown
capture_subprocess_io { @client.shutdown }
assert_predicate @client, :stopped?
end

Expand Down