Module: SafeTimeout

Defined in:
lib/safe_timeout.rb,
lib/safe_timeout/spawner.rb,
lib/safe_timeout/version.rb,
lib/safe_timeout/interrupting_child_process.rb

Overview

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.

Defined Under Namespace

Classes: InterruptingChildProcess, Spawner

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.send_signal(signal, pid) ⇒ Object



20
21
22
23
24
# 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) ⇒ Object



13
14
15
16
17
18
# 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