Repository navigation
Description
Thread.new never returns for a block that neither blocks nor finishes — the interpreter loop services no safepoint
One spinning Ruby thread freezes the whole interpreter, and it does it at
Thread.new, before any kill, sleep or Timeout is involved.
$stdout.sync = true
puts "a1-start"
spin = Thread.new { loop { } }
puts "a2-spawned alive=#{spin.alive?}"
n = 0
200_000.times { n += 1 }
puts "a3-main-did-interpreter-work n=#{n}"
sleep 0.2
puts "a4-main-returned-from-sleep"Run through cmd/oracle against MRI 4.0.7 and rbgo built from main at
bd5579c1:
| MRI 4.0.7 | rbgo | |
|---|---|---|
| exit | 0 | KILLED, it had not finished |
| stdout | 104 bytes, all four markers | 9 bytes: a1-start |
a2-spawned is the statement immediately after Thread.new. It never runs.
⛔ $stdout.sync = true and a marker per step are load-bearing here. An earlier
version of this probe produced zero bytes before being killed, which says
nothing about where it stopped: an absent line can be an unflushed line. The
localisation is only worth anything because the output is flushed as it happens.
The mechanism, read rather than inferred
eagerStart (internal/vm/thread.go:466) hands the GVL to the new thread and
parks the spawner on a channel:
func (vm *VM) eagerStart(t *RThread) {
cur := vm.currentThread
cur.saveCtx(vm)
vm.gvl.Unlock()
<-t.handback // <- the spawner waits here
vm.gvl.Lock()
cur.restoreCtx(vm)
}firstPark is the only sender on handback, and it has exactly two
callers — verified, not assumed:
| site | fires when the new thread… |
|---|---|
thread.go:438 (inside threadRelease) |
blocks or passes |
thread.go:616 (end of the spawn goroutine) |
finishes |
A thread that neither blocks nor finishes sends nothing, so eagerStart never
returns and Thread.new itself never returns. The eager-start handshake has
no third door, and its own comment at :616 says as much without noticing the
gap: "release the spawner if the thread never blocked".
⛔ And the third door is built, but the interpreter does not open it
serviceSafepoint / serviceSafepointAt exist and work. They are called from
19 sites, every one of them in thread.go or queue.go, and every one of
them a path that blocks or parks — join, Queue#pop, Mutex#lock,
Thread.stop, Thread.pass, the ioBlock release.
The interpreter's four jump sites call none of them:
internal/vm/vm.go:2588 case bytecode.OpJump:
internal/vm/vm.go:2591 case bytecode.OpBranchIf:
internal/vm/vm.go:2596 case bytecode.OpBranchUnless:
internal/vm/vm.go:2601 case bytecode.OpBranchNil:
And thread.go:925 already states the intent that was never carried out:
picked up at the target's next interpreter safepoint (a backward jump or a …)
So this is a mechanism wired at every door but the one a CPU-bound thread can
reach. Thread.pass shows the whole move already exists in two lines
(thread.go:694-697): vm.threadPass(runtime.Gosched) then
vm.serviceSafepoint(vm.currentThread).
What this explains, and what it does not
Measured with the same probe family:
| MRI | rbgo | ||
|---|---|---|---|
| kill a sleeping thread | dies | dies | already works — interruptibleWait |
| kill a CPU-bound thread | dies | program hangs | this issue |
Timeout.timeout(0.01) { sleep 1 } |
Timeout::Error |
block runs to completion | #803 |
Timeout.timeout(0.01) { 20_000_000.times { } } |
Timeout::Error |
block runs to completion | #803, and unreachable without this |
So this is the CPU half of #803 and of #933, and a candidate cause for #895.
Timeout.timeout cannot be implemented ahead of it: internal/vm/timeout.go:11
says so itself — "Timeout enforcement requires pre-emptively interrupting a
running block, which needs the scheduler work planned for a later round" — and
that round is this one.
⚠ It does not explain the library/socket hangs in #933: those are native
calls, where the GVL is released and the missing piece is a canceller per
site. Two different halves of one symptom, and conflating them would send the
next reader to the wrong file.
Shape of the fix, and the one thing that must not be skipped
At a backward jump (in.A <= pc, the only kind that can loop), periodically
do what Thread.pass does. Every N iterations, not every one.
⛔ This is the interpreter's hottest loop, so the change cannot land on a
correctness test alone. The repository's rule is that performance is part of
every change, measured against the reference, single core. Two designs are worth
measuring against each other rather than choosing by taste:
- A counter.
if vm.ticks++ >= N { vm.ticks = 0; … }— one increment and
one compare per backward jump, always paid. - A flag a timer sets, which is MRI's design (
rb_thread_scheduledriven by
the timer thread): one atomic load per backward jump, and the work happens
only when something is actually waiting.
N also trades latency against throughput, and "interrupt arrives within 0.1 s as
in MRI" (the bar #935 set for accept) is the property to hold while N grows.
I have not measured either, and will not today: this machine's 1-minute load
average has been between 36 and 90 all morning, and a throughput figure taken
there cannot tell a design apart from its neighbour.
Reproduction
cmd/oracle is in this repository and does exactly this comparison without
parsing anything:
go build -o /tmp/rbgo ./cmd/rbgo
go run ./scripts/conformance/rubyspec/cmd/oracle -script probe.rb \
/opt/homebrew/Cellar/ruby/4.0.7/bin/ruby /tmp/rbgoPrior art: priorart --repo go-embedded-ruby/ruby "a CPU-bound Ruby thread never yields the emulated GVL so every other thread starves" at 2026-11-15 11:37 CET
returned 1478 broad keyword matches. The adjacent entries — #803, #933, #916,
#759 — are all about interrupting a thread or about native calls; none is
about a spinning thread preventing Thread.new from returning. 45 branches
matched, all feature bindings and unrelated.
🤖 Generated with Claude Code
Prior art: priorart "a CPU-bound Ruby thread never yields the emulated GVL so every other thread starves" at 2026-11-15 11:37 CET reported prior art; checked: 1478 matches read: #803, #933, #916, #759 are all about INTERRUPTING a thread or about native calls where the GVL is already released. None covers a spinning Ruby thread stopping Thread.new itself from returning. #803 and #933 each have a CPU half unreachable until this lands, so this is their cause, not a duplicate. 45 branches, all feature bindings, none touching thread.go or the dispatch loop. Body carries the full list.
Activity
vm: test the safepoint branch the ratchet caught, which is the one th…
tannevaled commentedon Nov 15, 2026
Security reading: this is a denial of service by any untrusted Ruby, and there is currently no remedy on either side of the boundary
Stated separately from the mechanism above because it changes what this issue
is worth, not how it works. Every line below is either measured today or
read in the source, and marked as which.
The three remedies a host would reach for, and what each does
Timeout.timeout(n) { untrusted }ThreadcallingThread#killEvalreturnsThe third is the one that decides the severity. The public surface is
Neither takes a
context.Context, andInterphas noCancel,InterruptorStop.Evalcalls straight intovm.EvalIn(interp.go:79-85).Close(
interp.go:89) runs theat_exithandlers throughvm.Protect— it cannotinterrupt a running
Eval, and a concurrent call would contend for the GVL thespinning thread is holding.
⇒ What that adds up to
Interp.Eval("while true; end", …)wedges the calling goroutine permanently,and a Go host has no way to get it back. The only recourse is terminating the
process. No exploit, no crafted input — four words of ordinary Ruby.
And it is not limited to a deliberate loop: any untrusted expression that
happens not to terminate, or merely takes far longer than the host budgeted,
has the same effect, because the budget cannot be enforced from anywhere.
⚠ This is worth saying plainly because it is NOT inherent to embedding an
interpreter. MRI has the same shape of problem and three working answers:
Timeoutinterrupts,Thread#killreaches a CPU-bound thread, and its timerthread sets the interrupt flags that make both possible. rbgo has the
machinery —
serviceSafepointand 19 callers — and the interpreter loop opensnone of those doors, which is this issue.
Why it raises #942's priority rather than just its usefulness
#942 does not add a remedy. It adds the place a remedy can be delivered: a
safepoint in the dispatch loop and in
callBlockSelf, which is where aninterrupt — a
Thread#kill, aTimeout, or a future host-side cancellation —becomes deliverable at all. Measured there: with it,
Thread#killreaches aCPU-bound Ruby loop and
Thread.newreturns.So the order is forced, and it is the order #941's Phase 1 already puts it in:
#942 first because nothing else can work before it; then #803 (
Timeouton thenew primitive); then, as its own decision, whether the embedding API should take
a
context.Contextso a host can bound untrusted work without writing Ruby.⛔ No fix is proposed here and none should be read into it. This is the
audit note the repository's rules ask for — "audit untrusted input, resource
bounds … findings are fixed or filed, never silently accepted" — and the finding
is filed. Whether an embedder-facing cancellation API is in scope is a
maintainer's call, not mine.
⚠ One thing I did NOT test, so it is not claimed: whether a host that runs
Evalon its own goroutine and abandons it can keep operating with the VM inthat state. The GVL is held by the wedged thread, so any further
Evalon thesame
Interpwould be expected to block — expected, not measured.Refs #803, #942, #941
🤖 Generated with Claude Code