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
17 changes: 17 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,23 @@ 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 ForkPatch
#: () { () -> void } -> Integer?
def fork(&block)
super do
fork_caller = caller_locations(3, 1)&.first
$0 = "ruby-lsp-rails: #{fork_caller&.path}"
block.call
end
end

Kernel.prepend(self)
Process.singleton_class.prepend(self)
end

if ARGV.first == "start"
RubyLsp::Rails::Server.new(capabilities: JSON.parse(ARGV[1], symbolize_names: true)).start
end
32 changes: 32 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,38 @@ 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?

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

def execute(request, params)
pid = fork do
# We can't directly send a message in theses 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
File.write("process_name.txt", $0)
end

Process.wait(pid)
send_message({ process_name: File.read("process_name.txt") })
File.delete("process_name.txt")
end
end
RUBY

addon_path = File.expand_path("my_addon.rb")
@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}" })
ensure
FileUtils.rm("my_addon.rb")
end

private

def response
Expand Down
Loading