Module: Polyphony::TrapInterceptor

Included in:
Kernel, Kernel, Process
Defined in:
lib/polyphony/extensions/kernel.rb

Overview

Intercepts calls to #trap

Instance Method Summary collapse

Instance Method Details

#trap(sig, command = nil, &block) ⇒ Object

Installs a signal handler. If a block is given (or the command parameter is a Proc or a callable), it is executed inside an out-of-band, prioritized fiber.

If the command is the string “IGNORE” or “SIG_IGN”, the signal will be ignored. If the command is “DEFAULT” or “SIG_DFL”, the Ruby’s default handler will be invoked. If the command is “EXIT”, the script will be terminated by the signal. If the command is “SYSTEM_DEFAULT”, the operating system’s default handler will be invoked. Otherwise, the given command or block will be run. The special signal name “EXIT” or signal number zero will be invoked just prior to program termination.

trap returns the previous handler for the given signal.

Parameters:

  • sig (String, Symbol, Integer)

    signal name or number

  • command (String, Proc) (defaults to: nil)

    command to perform



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/polyphony/extensions/kernel.rb', line 24

def trap(sig, command = nil, &block)
  return super(sig, command) if command.is_a? String

  block = command if !block && command.respond_to?(:call)

  # The signal trap can be invoked at any time, including while the system
  # backend is blocking while polling for events. In order to deal with this
  # correctly, we run the signal handler code in an out-of-band, priority
  # scheduled fiber, that will pass any uncaught exception (including
  # SystemExit and Interrupt) to the main thread's main fiber. See also
  # `Fiber#schedule_priority_oob_fiber`.
  super(sig) do
    Fiber.schedule_priority_oob_fiber(&block)
  end
end