Skip to content

Commit

Permalink
Set process group ID in server to terminate after parent (#276)
Browse files Browse the repository at this point in the history
Prevent orphaned server process

Co-authored-by: Andy Waite <andyw8@users.noreply.github.com>
  • Loading branch information
vinistock and andyw8 authored Feb 29, 2024
1 parent 16395ec commit 019afe4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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

0 comments on commit 019afe4

Please sign in to comment.