module SafeTimeout

Ruby’s Timeout is broken and highly dangerous. To avoid this risk we instead use a child process to handle the timeout. We fork it and let it issue a SIGINT (Ctrl-C) to the parent if the timeout is reached.

Constants

VERSION

Public Class Methods

send_signal(signal, pid) click to toggle source
# File lib/safe_timeout.rb, line 20
def self.send_signal(signal, pid)
  Process.kill(signal, pid) if pid
rescue Errno::ESRCH
  # do nothing
end
timeout(sec, klass = nil, &block) click to toggle source
# File lib/safe_timeout.rb, line 13
def self.timeout(sec, klass = nil, &block)
  Spawner.new(
    timeout:    sec,
    on_timeout: ->(_) { raise(klass || Timeout::Error) },
  ).start(&block)
end