From: "hsbt (Hiroshi SHIBATA) via ruby-core" Date: 2024-12-03T06:49:37+00:00 Subject: [ruby-core:120085] [Ruby master Bug#20795] Timeout method doesn't check for negative time values Issue #20795 has been updated by hsbt (Hiroshi SHIBATA). We discussed this with Matz at last developer meeting. He said "Let's raise an `ArgumentError` against negative value". ---------------------------------------- Bug #20795: Timeout method doesn't check for negative time values https://bugs.ruby-lang.org/issues/20795#change-110834 * Author: kanakchaudhari12 (kanak chaudhari) * Status: Open * ruby -v: 3.1.0 * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- ```ruby 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 ```ruby def timeout(sec, klass = nil, message = nil, &block) raise ArgumentError, "timeout must be positive" if sec.is_a?(Numeric) && sec < 0 # ... end ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/