Skip to content

Conversation

@Cosmicoppai
Copy link
Contributor

 RUBY_VERSION
=> "3.2.3"
irb(main):006> require 'timeout'
=> true

# Case 1: Negative timeout with quick execution

irb(main):007* Timeout.timeout(-5) do
irb(main):008*   puts "This should not execute"
irb(main):009> end
This should not execute
=> nil

# Case 2: Negative timeout with sleep

irb(main):010* Timeout.timeout(-5) do
irb(main):011*   sleep(10)
irb(main):012> end
C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/timeout-0.4.1/lib/timeout.rb:43:in `rescue in handle_timeout': execution expired (Timeout::Error)
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/timeout-0.4.1/lib/timeout.rb:40:in `handle_timeout'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/timeout-0.4.1/lib/timeout.rb:195:in `timeout'
        from (irb):10:in `<main>'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/irb-1.12.0/exe/irb:9:in `<top (required)>'
        from C:/Ruby32-x64/bin/irb:25:in `load'
        from C:/Ruby32-x64/bin/irb:25:in `<main>'
(irb):11:in `sleep': execution expired (Timeout::ExitException)
        from (irb):11:in `block in <top (required)>'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/timeout-0.4.1/lib/timeout.rb:186:in `block in timeout'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/timeout-0.4.1/lib/timeout.rb:41:in `handle_timeout'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/timeout-0.4.1/lib/timeout.rb:195:in `timeout'
        from (irb):10:in `<main>'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/irb-1.12.0/exe/irb:9:in `<top (required)>'
        from C:/Ruby32-x64/bin/irb:25:in `load'
        from C:/Ruby32-x64/bin/irb:25:in `<main>'

 Timeout::VERSION
=> "0.4.1"

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

@eregon
Copy link
Member

eregon commented Oct 14, 2024

Thanks for the PR, could you add tests for this?

@Cosmicoppai Cosmicoppai requested a review from eregon October 16, 2024 18:01
@eregon
Copy link
Member

eregon commented Oct 16, 2024

Could you also update the docs?

Copy link
Contributor

@jeremyevans jeremyevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this. I agree we should raise for negative numbers, as you can make the argument that the timeout has already expired. It's arguable whether we should raise Timeout::Error (or klass) in this case, instead of ArgumentError, but I'm not opposed to using ArgumentError. I do have a couple inline requests for changes.

Cosmicoppai and others added 2 commits November 18, 2024 15:35
Co-authored-by: Jeremy Evans <[email protected]>
@jeremyevans
Copy link
Contributor

Sorry I missed approving this a few days ago. Thanks for the reminder!

@Cosmicoppai
Copy link
Contributor Author

@eregon whenever you find time, I’d greatly appreciate your review on the updated changes, thank you for your guidance!

@hsbt hsbt merged commit ad9bf8b into ruby:master Dec 3, 2024
@eregon
Copy link
Member

eregon commented Dec 3, 2024

LGTM, thanks for the review @jeremyevans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants