Project

General

Profile

Actions

Bug #20795

open

Timeout method doesn't check for negative time values

Added by kanakchaudhari12 (kanak chaudhari) about 1 month ago. Updated 3 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:119513]

Description

require 'timeout'

# Case 1: Negative timeout with quick execution
Timeout.timeout(-5) do
  puts "This should not execute"
end

# Case 2: Negative timeout with sleep
Timeout.timeout(-5) do
  sleep(10)
end

Potential issues with current behaviour:

  • Inconsistency: The behaviour differs based on the block's content, which may not be immediately apparent
  • Silent failure: The negative timeout is silently ignored, potentially masking logical errors in the calling code.
  • Unexpected source of error: one might expect the timeout method to validate its input, rather than relying on methods called within the block to catch invalid time values.

I suggest this change for the consistent behaviour regardless of code-block as well as the clear indication of the source of the error

def timeout(sec, klass = nil, message = nil, &block)
  raise ArgumentError, "timeout must be positive" if sec.is_a?(Numeric) && sec < 0
  # ...
end

Updated by Eregon (Benoit Daloze) about 1 month ago

Could you make a PR to https://github.com/ruby/timeout ? (or if not open an issue there?)

Actions

Also available in: Atom PDF

Like0
Like1Like0