Sha256: 18077a6c2313155459629855ca249c37be60b898d5f2fd765beef120df33a97c

Contents?: true

Size: 676 Bytes

Versions: 1

Compression:

Stored size: 676 Bytes

Contents

require 'timeout'

#
# 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.
#
module SafeTimeout

  autoload :InterruptingChildProcess, 'safe_timeout/interrupting_child_process'
  autoload :Spawner, 'safe_timeout/spawner'

  def self.timeout(sec, klass = nil, &block)
    Spawner.new(
      timeout:    sec,
      on_timeout: ->(_) { raise(klass || Timeout::Error) },
    ).start(&block)
  end

  def self.send_signal(signal, pid)
    Process.kill(signal, pid) if pid
  rescue Errno::ESRCH
    # do nothing
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
safe_timeout-1.0.0 lib/safe_timeout.rb