Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby32/share/ruby
File: timeout.rb
# frozen_string_literal: true
[0] Fix | Delete
# Timeout long-running blocks
[1] Fix | Delete
#
[2] Fix | Delete
# == Synopsis
[3] Fix | Delete
#
[4] Fix | Delete
# require 'timeout'
[5] Fix | Delete
# status = Timeout::timeout(5) {
[6] Fix | Delete
# # Something that should be interrupted if it takes more than 5 seconds...
[7] Fix | Delete
# }
[8] Fix | Delete
#
[9] Fix | Delete
# == Description
[10] Fix | Delete
#
[11] Fix | Delete
# Timeout provides a way to auto-terminate a potentially long-running
[12] Fix | Delete
# operation if it hasn't finished in a fixed amount of time.
[13] Fix | Delete
#
[14] Fix | Delete
# Previous versions didn't use a module for namespacing, however
[15] Fix | Delete
# #timeout is provided for backwards compatibility. You
[16] Fix | Delete
# should prefer Timeout.timeout instead.
[17] Fix | Delete
#
[18] Fix | Delete
# == Copyright
[19] Fix | Delete
#
[20] Fix | Delete
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
[21] Fix | Delete
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
[22] Fix | Delete
[23] Fix | Delete
module Timeout
[24] Fix | Delete
VERSION = "0.3.1"
[25] Fix | Delete
[26] Fix | Delete
# Raised by Timeout.timeout when the block times out.
[27] Fix | Delete
class Error < RuntimeError
[28] Fix | Delete
attr_reader :thread
[29] Fix | Delete
[30] Fix | Delete
def self.catch(*args)
[31] Fix | Delete
exc = new(*args)
[32] Fix | Delete
exc.instance_variable_set(:@thread, Thread.current)
[33] Fix | Delete
exc.instance_variable_set(:@catch_value, exc)
[34] Fix | Delete
::Kernel.catch(exc) {yield exc}
[35] Fix | Delete
end
[36] Fix | Delete
[37] Fix | Delete
def exception(*)
[38] Fix | Delete
# TODO: use Fiber.current to see if self can be thrown
[39] Fix | Delete
if self.thread == Thread.current
[40] Fix | Delete
bt = caller
[41] Fix | Delete
begin
[42] Fix | Delete
throw(@catch_value, bt)
[43] Fix | Delete
rescue UncaughtThrowError
[44] Fix | Delete
end
[45] Fix | Delete
end
[46] Fix | Delete
super
[47] Fix | Delete
end
[48] Fix | Delete
end
[49] Fix | Delete
[50] Fix | Delete
# :stopdoc:
[51] Fix | Delete
CONDVAR = ConditionVariable.new
[52] Fix | Delete
QUEUE = Queue.new
[53] Fix | Delete
QUEUE_MUTEX = Mutex.new
[54] Fix | Delete
TIMEOUT_THREAD_MUTEX = Mutex.new
[55] Fix | Delete
@timeout_thread = nil
[56] Fix | Delete
private_constant :CONDVAR, :QUEUE, :QUEUE_MUTEX, :TIMEOUT_THREAD_MUTEX
[57] Fix | Delete
[58] Fix | Delete
class Request
[59] Fix | Delete
attr_reader :deadline
[60] Fix | Delete
[61] Fix | Delete
def initialize(thread, timeout, exception_class, message)
[62] Fix | Delete
@thread = thread
[63] Fix | Delete
@deadline = GET_TIME.call(Process::CLOCK_MONOTONIC) + timeout
[64] Fix | Delete
@exception_class = exception_class
[65] Fix | Delete
@message = message
[66] Fix | Delete
[67] Fix | Delete
@mutex = Mutex.new
[68] Fix | Delete
@done = false # protected by @mutex
[69] Fix | Delete
end
[70] Fix | Delete
[71] Fix | Delete
def done?
[72] Fix | Delete
@mutex.synchronize do
[73] Fix | Delete
@done
[74] Fix | Delete
end
[75] Fix | Delete
end
[76] Fix | Delete
[77] Fix | Delete
def expired?(now)
[78] Fix | Delete
now >= @deadline
[79] Fix | Delete
end
[80] Fix | Delete
[81] Fix | Delete
def interrupt
[82] Fix | Delete
@mutex.synchronize do
[83] Fix | Delete
unless @done
[84] Fix | Delete
@thread.raise @exception_class, @message
[85] Fix | Delete
@done = true
[86] Fix | Delete
end
[87] Fix | Delete
end
[88] Fix | Delete
end
[89] Fix | Delete
[90] Fix | Delete
def finished
[91] Fix | Delete
@mutex.synchronize do
[92] Fix | Delete
@done = true
[93] Fix | Delete
end
[94] Fix | Delete
end
[95] Fix | Delete
end
[96] Fix | Delete
private_constant :Request
[97] Fix | Delete
[98] Fix | Delete
def self.create_timeout_thread
[99] Fix | Delete
watcher = Thread.new do
[100] Fix | Delete
requests = []
[101] Fix | Delete
while true
[102] Fix | Delete
until QUEUE.empty? and !requests.empty? # wait to have at least one request
[103] Fix | Delete
req = QUEUE.pop
[104] Fix | Delete
requests << req unless req.done?
[105] Fix | Delete
end
[106] Fix | Delete
closest_deadline = requests.min_by(&:deadline).deadline
[107] Fix | Delete
[108] Fix | Delete
now = 0.0
[109] Fix | Delete
QUEUE_MUTEX.synchronize do
[110] Fix | Delete
while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
[111] Fix | Delete
CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
[112] Fix | Delete
end
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
requests.each do |req|
[116] Fix | Delete
req.interrupt if req.expired?(now)
[117] Fix | Delete
end
[118] Fix | Delete
requests.reject!(&:done?)
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
ThreadGroup::Default.add(watcher)
[122] Fix | Delete
watcher.name = "Timeout stdlib thread"
[123] Fix | Delete
watcher.thread_variable_set(:"\0__detached_thread__", true)
[124] Fix | Delete
watcher
[125] Fix | Delete
end
[126] Fix | Delete
private_class_method :create_timeout_thread
[127] Fix | Delete
[128] Fix | Delete
def self.ensure_timeout_thread_created
[129] Fix | Delete
unless @timeout_thread and @timeout_thread.alive?
[130] Fix | Delete
TIMEOUT_THREAD_MUTEX.synchronize do
[131] Fix | Delete
unless @timeout_thread and @timeout_thread.alive?
[132] Fix | Delete
@timeout_thread = create_timeout_thread
[133] Fix | Delete
end
[134] Fix | Delete
end
[135] Fix | Delete
end
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
# We keep a private reference so that time mocking libraries won't break
[139] Fix | Delete
# Timeout.
[140] Fix | Delete
GET_TIME = Process.method(:clock_gettime)
[141] Fix | Delete
private_constant :GET_TIME
[142] Fix | Delete
[143] Fix | Delete
# :startdoc:
[144] Fix | Delete
[145] Fix | Delete
# Perform an operation in a block, raising an error if it takes longer than
[146] Fix | Delete
# +sec+ seconds to complete.
[147] Fix | Delete
#
[148] Fix | Delete
# +sec+:: Number of seconds to wait for the block to terminate. Any number
[149] Fix | Delete
# may be used, including Floats to specify fractional seconds. A
[150] Fix | Delete
# value of 0 or +nil+ will execute the block without any timeout.
[151] Fix | Delete
# +klass+:: Exception Class to raise if the block fails to terminate
[152] Fix | Delete
# in +sec+ seconds. Omitting will use the default, Timeout::Error
[153] Fix | Delete
# +message+:: Error message to raise with Exception Class.
[154] Fix | Delete
# Omitting will use the default, "execution expired"
[155] Fix | Delete
#
[156] Fix | Delete
# Returns the result of the block *if* the block completed before
[157] Fix | Delete
# +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
[158] Fix | Delete
#
[159] Fix | Delete
# The exception thrown to terminate the given block cannot be rescued inside
[160] Fix | Delete
# the block unless +klass+ is given explicitly. However, the block can use
[161] Fix | Delete
# ensure to prevent the handling of the exception. For that reason, this
[162] Fix | Delete
# method cannot be relied on to enforce timeouts for untrusted blocks.
[163] Fix | Delete
#
[164] Fix | Delete
# If a scheduler is defined, it will be used to handle the timeout by invoking
[165] Fix | Delete
# Scheduler#timeout_after.
[166] Fix | Delete
#
[167] Fix | Delete
# Note that this is both a method of module Timeout, so you can <tt>include
[168] Fix | Delete
# Timeout</tt> into your classes so they have a #timeout method, as well as
[169] Fix | Delete
# a module method, so you can call it directly as Timeout.timeout().
[170] Fix | Delete
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
[171] Fix | Delete
return yield(sec) if sec == nil or sec.zero?
[172] Fix | Delete
[173] Fix | Delete
message ||= "execution expired"
[174] Fix | Delete
[175] Fix | Delete
if Fiber.respond_to?(:current_scheduler) && (scheduler = Fiber.current_scheduler)&.respond_to?(:timeout_after)
[176] Fix | Delete
return scheduler.timeout_after(sec, klass || Error, message, &block)
[177] Fix | Delete
end
[178] Fix | Delete
[179] Fix | Delete
Timeout.ensure_timeout_thread_created
[180] Fix | Delete
perform = Proc.new do |exc|
[181] Fix | Delete
request = Request.new(Thread.current, sec, exc, message)
[182] Fix | Delete
QUEUE_MUTEX.synchronize do
[183] Fix | Delete
QUEUE << request
[184] Fix | Delete
CONDVAR.signal
[185] Fix | Delete
end
[186] Fix | Delete
begin
[187] Fix | Delete
return yield(sec)
[188] Fix | Delete
ensure
[189] Fix | Delete
request.finished
[190] Fix | Delete
end
[191] Fix | Delete
end
[192] Fix | Delete
[193] Fix | Delete
if klass
[194] Fix | Delete
perform.call(klass)
[195] Fix | Delete
else
[196] Fix | Delete
backtrace = Error.catch(&perform)
[197] Fix | Delete
raise Error, message, backtrace
[198] Fix | Delete
end
[199] Fix | Delete
end
[200] Fix | Delete
module_function :timeout
[201] Fix | Delete
end
[202] Fix | Delete
[203] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function