Skip to content

Automatically name forked processes based on callers #633

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,25 @@ def database_supports_indexing?(model)
end
end

# Patch fork to name processes based on the caller's file path. This is useful for figuring out what is creating more
# child processes from the runtime server, so that we can optimize and more easily debug orphaned processes
# @requires_ancestor: Kernel
module ForkHook
#: (*untyped) -> Integer?
def _fork(*args)
pid = super

if pid == 0
fork_caller = caller_locations(1, 1)&.first
Process.setproctitle("ruby-lsp-rails: #{fork_caller.path}") if fork_caller
end

pid
end

Process.singleton_class.prepend(self)
end

if ARGV.first == "start"
RubyLsp::Rails::Server.new(capabilities: JSON.parse(ARGV[1], symbolize_names: true)).start
end
80 changes: 80 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ServerTest < ActiveSupport::TestCase
setup do
@stdout = StringIO.new
@stderr = StringIO.new
RubyLsp::Rails::ServerAddon.instance_variable_set(:@server_addon_classes, [])
RubyLsp::Rails::ServerAddon.instance_variable_set(:@server_addons, {})
@server = RubyLsp::Rails::Server.new(stdout: @stdout, stderr: @stderr, override_default_output_device: false)
end

Expand Down Expand Up @@ -268,6 +270,84 @@ def print_it!
$> = original_stdout
end

test "forked processes are named based on caller" do
skip("Fork is not supported on Windows") if Gem.win_platform?

addon_path = File.expand_path("my_addon.rb")
File.write(addon_path, <<~RUBY)
class MyServerAddon < RubyLsp::Rails::ServerAddon
def name
"MyAddon"
end

def execute(request, params)
parent_process_title = `ps -p \#{Process.pid} -o args=`.lines.last.strip
file = "process_name.txt"
pid = fork do
# We can't directly send a message in these tests because we're using a StringIO as stdout instead of the
# actual pipe, which means that the child process doesn't have access to the same object
process_title = `ps -p \#{Process.pid} -o args=`.lines.last.strip
File.write(file, process_title)
end

Process.wait(pid)

parent_process_title_changed = `ps -p \#{Process.pid} -o args=`.lines.last.strip != parent_process_title
send_message({ process_name: File.read(file), changed_parent_title: parent_process_title_changed })
File.delete(file)
end
end
RUBY

begin
@server.execute("server_addon/register", server_addon_path: addon_path)
@server.execute("server_addon/delegate", server_addon_name: "MyAddon", request_name: "dsl")
assert_equal(response, { process_name: "ruby-lsp-rails: #{addon_path}", changed_parent_title: false })
ensure
FileUtils.rm(addon_path)
end
end

test "forked processes with no block are named based on caller" do
skip("Fork is not supported on Windows") if Gem.win_platform?

addon_path = File.expand_path("my_other_addon.rb")
File.write(addon_path, <<~RUBY)
class MyOtherServerAddon < RubyLsp::Rails::ServerAddon
def name
"MyOtherAddon"
end

def execute(request, params)
parent_process_title = `ps -p \#{Process.pid} -o args=`.lines.last.strip
file = "other_process_name.txt"
pid = fork

if pid
Process.wait(pid)
parent_process_title_changed = `ps -p \#{Process.pid} -o args=`.lines.last.strip != parent_process_title
send_message({ process_name: File.read(file), changed_parent_title: parent_process_title_changed })
File.delete(file)
else
process_title = `ps -p \#{Process.pid} -o args=`.lines.last.strip
File.write(file, process_title)

# Exit from the child process or else we're stuck in the infinite loop of the server
exit!
end
end
end
RUBY

begin
@server.execute("server_addon/register", server_addon_path: addon_path)
@server.execute("server_addon/delegate", server_addon_name: "MyOtherAddon", request_name: "dsl")
assert_equal(response, { process_name: "ruby-lsp-rails: #{addon_path}", changed_parent_title: false })
ensure
FileUtils.rm(addon_path)
end
end

private

def response
Expand Down
Loading