Skip to content

vm: Timeout.timeout enforces its deadline on a blocking block - #988

Merged
tannevaled merged 1 commit into
mainfrom
timeout-enforced
Nov 29, 2026
Merged

tannevaled merged 1 commit into
mainfrom
timeout-enforced

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Refs #803
Refs #940
Refs #942

What

Timeout.timeout(n) { … } now enforces n on 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.

program MRI 4.0.10 rbgo main (ed535866) this branch
Timeout.timeout(0.3) { sleep 2 } Timeout::Error after 0.3 s returns after 2 s Timeout::Error after 0.3 s
Timeout.timeout(0.3, ArgumentError, "custom") { Queue.new.pop } ArgumentError: custom hangs forever ArgumentError: custom
Timeout.timeout(5) { |s| p s } 5 nil 5
bare rescue inside the block does not catch the timeout n/a does not catch it

How

  • Timeout.timeout is the timeout gem 0.6.0 that MRI 4.0.10 bundles (lib/timeout.rb at v4.0.10, Ruby's license), ported into prelude.rb without its Ractor branch.
  • A watcher thread holds the pending requests. At a deadline it calls Thread#raise on the requesting thread, and rbgo's Thread#raise already wakes sleep, Queue#pop, Mutex#lock and IO waits (measured: AGREE with MRI).
  • Without an exception class, the block sees Timeout::ExitException < Exception, which handle_timeout turns into Timeout::Error at the call.
  • nil / 0 mean no deadline; a negative limit raises ArgumentError. Timeout::VERSION is "0.6.0", and include Timeout gives a private #timeout.
  • The Timeout module and Timeout::Error stay in Go (timeout.go), because ConnectionPool's Go side subclasses Timeout::Error before the prelude runs.
  • prelude_frozen_gen.go was regenerated with go generate ./internal/vm/ (cmd/freeze-prelude).

What this does NOT fix (why Refs, not Closes)

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.10 on a 15-line probe covering every row above, plus nil/0/negative limits, ExitException's ancestry, Timeout::VERSION, include Timeout and Thread.list.size:
    • this branch: AGREE, 1.19 s vs MRI 1.19 s;
    • main: TIMED OUT after 30 s.
  • TestTimeout was rewritten: it pinned the nil yield, 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. TestTimeoutIsEnforced is 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.
  • 83 Timeout/Prelude/Frozen/ConnectionPool/NATS/Thread tests pass locally.
  • gofmt -l: clean. go generate also reflowed aot_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 Exception or use ensure to outlive its deadline, in MRI as here.

🤖 Generated with Claude Code

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>
@tannevaled
tannevaled merged commit d6fc2a4 into main Nov 29, 2026
26 checks passed
@tannevaled
tannevaled deleted the timeout-enforced branch November 29, 2026 16:59
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.

1 participant