Skip to content

Commit c53dd0e

Browse files
committed
Prevent orphaned server process
1 parent 56d04dc commit c53dd0e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def initialize
3131
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
3232
# set its own session ID
3333
begin
34+
Process.setpgrp
3435
Process.setsid
3536
rescue Errno::EPERM
3637
# If we can't set the session ID, continue
@@ -52,6 +53,14 @@ def initialize
5253
warn("Ruby LSP Rails booting server")
5354
read_response
5455
warn("Finished booting Ruby LSP Rails server")
56+
57+
unless ENV["RAILS_ENV"] == "test"
58+
at_exit do
59+
warn("Ruby LSP Rails is force killing the server")
60+
sleep(0.5) # give the server a bit of time if we already issued a shutdown notification
61+
Process.kill(T.must(Signal.list["TERM"]), @wait_thread.pid) if @wait_thread.alive?
62+
end
63+
end
5564
rescue Errno::EPIPE, IncompleteMessageError
5665
raise InitializationError, @stderr.read
5766
end
@@ -66,8 +75,9 @@ def model(name)
6675

6776
sig { void }
6877
def shutdown
69-
send_notification("shutdown")
70-
Thread.pass while @wait_thread.alive?
78+
warn("Ruby LSP Rails shutting down server")
79+
send_message("shutdown")
80+
sleep(0.5) # give the server a bit of time to shutdown
7181
[@stdin, @stdout, @stderr].each(&:close)
7282
end
7383

test/ruby_lsp_rails/runner_client_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ module RubyLsp
88
module Rails
99
class RunnerClientTest < ActiveSupport::TestCase
1010
setup do
11-
capture_io do
11+
capture_subprocess_io do
1212
@client = T.let(RunnerClient.new, RunnerClient)
1313
end
1414
end
1515

1616
teardown do
17-
@client.shutdown
17+
capture_subprocess_io { @client.shutdown }
1818
assert_predicate @client, :stopped?
1919
end
2020

0 commit comments

Comments
 (0)