Skip to content

Instantly share code, notes, and snippets.

@jjb
Created Sep 11, 2021
Embed
What would you like to do?
Exploring the default behavior for signals in ruby

Ruby doesn't let you inherit default behavior when writing a signal trap

Here's a beginning of an exploration of what default behavior is for each signal:

# https://github.com/ruby/ruby/blob/master/signal.c#L1331-L1358
reserved = %w[SEGV BUS ILL FPE VTALRM]
reserved += %w[KILL STOP] # not listed in code but reserved via some other mechanism i was too lazy to find

Signal.list.keys.each do |signal|
  next if reserved.include? signal
  old_handler = Signal.trap(signal) do
    puts "Received #{signal}"
    exit
  end

  puts "#{signal}: #{old_handler}"
end
EXIT:
HUP: DEFAULT
INT: DEFAULT
QUIT: DEFAULT
TRAP: SYSTEM_DEFAULT
ABRT: SYSTEM_DEFAULT
IOT: #<Proc:0x000000014b8462e0 code.rb:7>
EMT: SYSTEM_DEFAULT
SYS:
PIPE:
ALRM: DEFAULT
TERM: DEFAULT
URG: SYSTEM_DEFAULT
TSTP: SYSTEM_DEFAULT
CONT: SYSTEM_DEFAULT
CHLD: DEFAULT
CLD: #<Proc:0x000000014b8168b0 code.rb:7>
TTIN: SYSTEM_DEFAULT
TTOU: SYSTEM_DEFAULT
IO: SYSTEM_DEFAULT
XCPU: SYSTEM_DEFAULT
XFSZ: SYSTEM_DEFAULT
PROF: SYSTEM_DEFAULT
WINCH: SYSTEM_DEFAULT
USR1: DEFAULT
USR2: DEFAULT
INFO: SYSTEM_DEFAULT
Received EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment