enum

Signal

Inherits Enum / Comparable / Value / Object

Safely handle inter-process signals on POSIX systems.

Signals are dispatched to the event loop and later processed in a dedicated fiber. Some received signals may never be processed when the program terminates.

puts "Ctrl+C still has the OS default action (stops the program)"
sleep 3.seconds

Signal::INT.trap do
  puts "Gotcha!"
end
puts "Ctrl+C will be caught from now on"
sleep 3.seconds

Signal::INT.reset
puts "Ctrl+C is back to the OS default action"
sleep 3.seconds

WARNING: An uncaught exception in a signal handler is a fatal error.

Portability

The set of available signals is platform-dependent. Only signals that exist on the target platform are available as members of this enum.

  • ABRT, FPE, ILL, INT, SEGV, and TERM are guaranteed to exist on all platforms.
  • PWR, STKFLT, and UNUSED only exist on Linux.
  • BREAK only exists on Windows.
  • All other signals exist on all POSIX platforms.

The methods #trap, #reset, and #ignore may not be implemented at all on non-POSIX systems.

The standard library provides several platform-agnostic APIs to achieve tasks that are typically solved with signals on POSIX systems:

  • The portable API for responding to a termination request is Process.on_terminate.
  • The portable API for sending a TERM or KILL signal to a process is Process#terminate.
  • The portable API for retrieving the exit signal of a process (Process::Status#exit_signal) is Process::Status#exit_reason.

Constants

INT = 2
ILL = 4
FPE = 8
SEGV = 11
TERM = 15
ABRT = 6
HUP = 1
QUIT = 3
TRAP = 5
IOT = 6
KILL = 9
BUS = 7
SYS = 31
PIPE = 13
ALRM = 14
URG = 23
STOP = 19
TSTP = 20
CONT = 18
CHLD = 17
TTIN = 21
TTOU = 22
IO = 29
XCPU = 24
XFSZ = 25
VTALRM = 26
USR1 = 10
USR2 = 12
WINCH = 28
PWR = 30
STKFLT = 16
UNUSED = 31

Instance methods

abrt?

Returns true if this enum value equals ABRT

Source
alrm?

Returns true if this enum value equals ALRM

Source
bus?

Returns true if this enum value equals BUS

Source
chld?

Returns true if this enum value equals CHLD

Source
cont?

Returns true if this enum value equals CONT

Source
fpe?

Returns true if this enum value equals FPE

Source
hup?

Returns true if this enum value equals HUP

Source
ignore

Clears the handler for this signal and prevents the OS default action.

Note that trying to ignore CHLD will actually set the default crystal handler that monitors and reaps child processes. This prevents zombie processes and is required by Process#wait for example.

Source
ill?

Returns true if this enum value equals ILL

Source
int?

Returns true if this enum value equals INT

Source
io?

Returns true if this enum value equals IO

Source
iot?

Returns true if this enum value equals IOT

Source
kill?

Returns true if this enum value equals KILL

Source
pipe?

Returns true if this enum value equals PIPE

Source
pwr?

Returns true if this enum value equals PWR

Source
quit?

Returns true if this enum value equals QUIT

Source
reset

Resets the handler for this signal to the OS default.

Note that trying to reset CHLD will actually set the default crystal handler that monitors and reaps child processes. This prevents zombie processes and is required by Process#wait for example.

Source
segv?

Returns true if this enum value equals SEGV

Source
stkflt?

Returns true if this enum value equals STKFLT

Source
stop?

Returns true if this enum value equals STOP

Source
sys?

Returns true if this enum value equals SYS

Source
term?

Returns true if this enum value equals TERM

Source
trap

Sets the handler for this signal to the passed function.

After executing this, whenever the current process receives the corresponding signal, the passed function will be called (instead of the OS default). The handler will run in a signal-safe fiber throughout the event loop; there is no limit to what functions can be called, unlike raw signals that run on the sigaltstack.

Note that CHLD is always trapped and child processes will always be reaped before the custom handler is called, hence a custom CHLD handler must check child processes using Process.exists?. Trying to use waitpid with a zero or negative value won't work.

NOTE: Process.on_terminate is preferred over Signal::INT.trap as a portable alternative which also works on Windows.

Source
trap?

Returns true if this enum value equals TRAP

Source
trap_handler?

Returns any existing handler for this signal

Signal::USR1.trap { }
prev_handler = Signal::USR1.trap_handler?

Signal::USR1.trap do |signal|
  prev_handler.try &.call(signal)
  # ...
end
Source
tstp?

Returns true if this enum value equals TSTP

Source
ttin?

Returns true if this enum value equals TTIN

Source
ttou?

Returns true if this enum value equals TTOU

Source
unused?

Returns true if this enum value equals UNUSED

Source
urg?

Returns true if this enum value equals URG

Source
usr1?

Returns true if this enum value equals USR1

Source
usr2?

Returns true if this enum value equals USR2

Source
vtalrm?

Returns true if this enum value equals VTALRM

Source
winch?

Returns true if this enum value equals WINCH

Source
xcpu?

Returns true if this enum value equals XCPU

Source
xfsz?

Returns true if this enum value equals XFSZ

Source