Repository navigation
vm: Timeout.timeout enforces its deadline on a blocking block - #988
Merged
Merged
Conversation
Timeout.timeout ignored its limit: the block ran to completion, or forever, and the call returned normally. Code that bounded untrusted or hanging work with it was unprotected, and silently so. It also yielded nil where MRI yields the limit. Timeout.timeout is now the timeout gem 0.6.0 that MRI 4.0.10 bundles (lib/timeout.rb, Ruby's license), ported into the prelude minus its Ractor branch. A watcher thread holds the pending requests and, at a deadline, interrupts the requesting thread with Thread#raise, which already wakes sleep, Queue#pop, Mutex#lock and IO waits. Without an exception class the block sees Timeout::ExitException (an Exception, so a bare rescue does not eat it), and handle_timeout turns it into Timeout::Error at the call. nil and 0 mean no deadline; a negative limit is an ArgumentError; Timeout::VERSION is "0.6.0"; include Timeout gives a private #timeout. The Timeout module and Timeout::Error stay in Go, because ConnectionPool subclasses Timeout::Error before the prelude runs. A block that spins without blocking is still not interrupted: the watcher is itself a Ruby thread and never gets the GVL from a spinning one, which is #940. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #803
Refs #940
Refs #942
What
Timeout.timeout(n) { … }now enforcesnon a block that blocks. Before, the deadline was ignored: the block ran to completion, or forever, and the call returned normally. That was #803's security point: code bounding hanging work was unprotected, and nothing said so.main(ed535866)Timeout.timeout(0.3) { sleep 2 }Timeout::Errorafter 0.3 sTimeout::Errorafter 0.3 sTimeout.timeout(0.3, ArgumentError, "custom") { Queue.new.pop }ArgumentError: customArgumentError: customTimeout.timeout(5) { |s| p s }5nil5rescueinside the blockHow
Timeout.timeoutis the timeout gem 0.6.0 that MRI 4.0.10 bundles (lib/timeout.rbatv4.0.10, Ruby's license), ported intoprelude.rbwithout its Ractor branch.Thread#raiseon the requesting thread, and rbgo'sThread#raisealready wakessleep,Queue#pop,Mutex#lockand IO waits (measured: AGREE with MRI).Timeout::ExitException < Exception, whichhandle_timeoutturns intoTimeout::Errorat the call.nil/0mean no deadline; a negative limit raisesArgumentError.Timeout::VERSIONis"0.6.0", andinclude Timeoutgives a private#timeout.Timeoutmodule andTimeout::Errorstay in Go (timeout.go), because ConnectionPool's Go side subclassesTimeout::Errorbefore the prelude runs.prelude_frozen_gen.gowas regenerated withgo generate ./internal/vm/(cmd/freeze-prelude).What this does NOT fix (why
Refs, notCloses)A block that spins without blocking (
loop { x += 1 }) is still not interrupted. The watcher is a Ruby thread and never gets the GVL back from a spinning one; measured, it times out at 30 s. That is #940, whose fix is the safepoint in #942. Once a spinning thread yields at a safepoint, this same code interrupts it with no change here.Evidence
rubydiff -a rbgo -b ruby 4.0.10on a 15-line probe covering every row above, plus nil/0/negative limits,ExitException's ancestry,Timeout::VERSION,include TimeoutandThread.list.size:main: TIMED OUT after 30 s.TestTimeoutwas rewritten: it pinned thenilyield, which was the defect (Timeout.timeout never times out: it runs the block to completion and returns, so code that bounds untrusted work is unprotected and silent about it #803 said so), and its no-block case called the old Go native.TestTimeoutIsEnforcedis new. Expectations are MRI 4.0.10's output; time bounds are loose (under 1.9 s against a 2 s sleep), so a loaded machine cannot flake them.gofmt -l: clean.go generatealso reflowedaot_e2e_gen_test.go, an unrelated generator whose output is not gofmt'd; that file was re-gofmt'd back and is not in this PR.Security
The fix narrows #803's fail-open. The remaining fail-open, a CPU-bound block, is #940 / #942. The gem's own documentation applies too: a block can still
rescue Exceptionor useensureto outlive its deadline, in MRI as here.🤖 Generated with Claude Code