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 =
"0.0.4"
Class Method Summary collapse
Class Method Details
.send_signal(signal, pid) ⇒ Object
18 19 20 21 |
# File 'lib/safe_timeout.rb', line 18 def self.send_signal(signal, pid) Process.kill(signal, pid) if pid rescue Errno::ESRCH end |
.timeout(sec, klass = nil, &block) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/safe_timeout.rb', line 11 def self.timeout(sec, klass=nil, &block) Spawner.new( :timeout => sec, :on_timeout => lambda { |_| raise(klass || Timeout::Error) } ).start(&block) end |