Project

General

Profile

Actions

Bug #20795

closed

Timeout method doesn't check for negative time values

Added by kanakchaudhari12 (kanak chaudhari) 2 months ago. Updated 19 days ago.

Status:
Closed
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
Actions

Also available in: Atom PDF

Like0
Like1Like0Like0Like0