Skip to content

Commit 12b2674

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

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 14 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,16 @@ 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+
if @wait_thread.alive?
60+
warn("Ruby LSP Rails is force killing the server")
61+
sleep(0.5) # give the server a bit of time if we already issued a shutdown notification
62+
Process.kill(T.must(Signal.list["TERM"]), @wait_thread.pid)
63+
end
64+
end
65+
end
5566
rescue Errno::EPIPE, IncompleteMessageError
5667
raise InitializationError, @stderr.read
5768
end
@@ -66,8 +77,9 @@ def model(name)
6677

6778
sig { void }
6879
def shutdown
69-
send_notification("shutdown")
70-
Thread.pass while @wait_thread.alive?
80+
warn("Ruby LSP Rails shutting down server")
81+
send_message("shutdown")
82+
sleep(0.5) # give the server a bit of time to shutdown
7183
[@stdin, @stdout, @stderr].each(&:close)
7284
end
7385

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)