Skip to content
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
6 changes: 6 additions & 0 deletions lib/vernier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative "vernier/stack_table"
require_relative "vernier/result"
require_relative "vernier/hooks"
require_relative "vernier/fork"
require_relative "vernier/vernier"
require_relative "vernier/output/firefox"
require_relative "vernier/output/top"
Expand Down Expand Up @@ -56,6 +57,11 @@ def self.stop_profile
result
end

def self.cancel_profile
@collector&.cancel
@collector = nil
end

def self.trace_retained(**profile_options, &block)
profile(**profile_options.merge(mode: :retained), &block)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/vernier/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def record_interval(category, name = category)
)
end

def cancel
finish
@thread_names.cancel
@hooks.each do |hook|
hook.disable
end
nil
end

def stop
result = finish

Expand Down
17 changes: 17 additions & 0 deletions lib/vernier/fork.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Vernier
if ::Process.respond_to?(:_fork)
module ForkHooks
def _fork
pid = super
if pid == 0 # We're in the child
Vernier.cancel_profile
end
pid
end
end

::Process.singleton_class.prepend(ForkHooks)
end
end
4 changes: 4 additions & 0 deletions lib/vernier/thread_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def [](object_id)
@names[object_id] || "thread obj_id:#{object_id}"
end

def cancel
@tp.disable
end

def finish
collect_running
@tp.disable
Expand Down
8 changes: 8 additions & 0 deletions test/test_vernier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ class TestVernier < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::Vernier::VERSION
end

def test_that_forked_children_do_not_hang
pid = Process.fork do
# noop
end
_, status = Process.waitpid2(pid)
assert_predicate status, :success?
end
end
Loading