Module: Homebrew::VernierForkGuard Private

Extended by:
T::Sig
Defined in:
prof/vernier_fork_guard.rb

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Class Method Summary collapse

Methods included from T::Sig

sig

Class Method Details

.stop_running_collectorvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



46
47
48
49
50
51
52
53
# File 'prof/vernier_fork_guard.rb', line 46

def self.stop_running_collector
  return unless Object.const_defined?(:Vernier)

  autorun = T.let(Object.const_get(:Vernier).const_get(:Autorun), T.untyped)
  return unless autorun.running?

  autorun.stop
end

.without_running_collector(&block) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'prof/vernier_fork_guard.rb', line 15

def self.without_running_collector(&block)
  raise ArgumentError, "block required" unless block

  return yield unless Object.const_defined?(:Vernier)

  # `Vernier::Autorun` is created by `-r vernier/autorun`; Sorbet's RBI for
  # the gem does not expose it, so keep this lookup dynamic.
  autorun = T.let(Object.const_get(:Vernier).const_get(:Autorun), T.untyped)
  return yield unless autorun.running?

  # Vernier registers internal thread hooks and owns native mutexes while the
  # collector is running. Forking with that state active can leave the child
  # process stuck before it reaches exec.
  #
  # Stopping here loses samples taken during fork setup, but that is a better
  # tradeoff than hanging the profiled command. The common process helpers use
  # spawn while `HOMEBREW_SPAWN_SYSTEM` is set, so this remains a fallback.
  autorun.collector.stop
  autorun.collector = nil
  pid = nil
  begin
    pid = yield
  ensure
    # Restart only in the parent. In the child, `yield` returns nil and exec
    # should happen immediately through the original fork path.
    autorun.start if pid && !autorun.running?
  end
  pid
end