From 0627f0966254b4407df75071f58a22dd514e12a1 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 15 Nov 2026 12:02:18 +0100 Subject: [PATCH 1/2] vm: the interpreter services a safepoint, so a spinning thread stops freezing it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #940 measured it: `Thread.new { while true; end }` and Thread.new ITSELF never returns. eagerStart parks the spawner on a channel until the new thread blocks or finishes, and firstPark -- the only sender -- has exactly those two callers. A bare loop takes neither. The safepoint machinery was already complete: serviceSafepoint is called from 19 sites, every one in thread.go or queue.go, every one a path that blocks or parks. The interpreter called it from none, and thread.go's own comment said a backward jump was meant to be one. ## TWO doors, and wiring one was measured to do nothing Door 1, the dispatch loop's backward edges (OpJump, OpBranchIf, OpBranchUnless, OpBranchNil, guarded by in.A <= pc). That alone made `while true; end` agree with MRI byte for byte. Door 2, callBlockSelf. And `loop { }` went on hanging EXACTLY as before, byte for byte against the control, because Kernel#loop, Integer#times and Array#each spin in a GO loop calling the block: their bytecode has no backward jump, so door 1 never fires. callBlockSelf is the funnel every native iterator passes through. A fix that does not move the measurement is not a fix, and this one did not until door 2 was wired. Both feed one vm.tickSafepoint. ## Measured with reference, control and treatment cmd/oracle, three engines: MRI 4.0.7, rbgo at main, this branch. Thread.new { while true; end } MRI 88 bytes / main KILLED 9 bytes / branch AGREES byte for byte Thread.new { loop { } } MRI 104 bytes / main KILLED 9 bytes / branch AGREES byte for byte So Thread.new returns, the main thread runs beside a spinning one, and Thread#kill reaches a CPU-bound Ruby loop. ## What this does NOT fix, measured rather than assumed #803 Timeout.timeout is byte-identical between control and treatment: timeout.go is still the stub whose comment says enforcement needs scheduler work. This is that work, and Timeout now has its primitive, but nothing here implements it -- its own change, so a Timeout regression is never diagnosed through a hot-loop change. #933's library/socket hangs are the other half: native calls where the GVL IS released and a canceller per site is missing. ## The performance measurement is OWED This touches the hottest loop plus every block invocation. safepointTicks = 4096 is a starting point, NOT a measured value, and two designs need measuring against each other: a counter paid per tick (this one) versus MRI's timer-set flag and a single load. The 1-minute load average here has been 36-90 all morning, where a throughput figure cannot tell one design from another. ⛔ Do not merge on the green suite alone. Also corrected: vm.go's GVL comment ended at "released only inside blocking native methods", and that word was the whole defect -- the list was accurate and its COMPLETENESS was what nobody checked. Refs #940, #803, #933 Co-Authored-By: Claude Opus 5 --- internal/vm/object_model.go | 8 ++++++ internal/vm/thread.go | 57 +++++++++++++++++++++++++++++++++++++ internal/vm/vm.go | 41 ++++++++++++++++++++++++-- 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/internal/vm/object_model.go b/internal/vm/object_model.go index 10dcee3c..41348a18 100644 --- a/internal/vm/object_model.go +++ b/internal/vm/object_model.go @@ -1817,6 +1817,14 @@ func (vm *VM) callBlock(p *Proc, args []object.Value) object.Value { // callBlockSelf is callBlock with an explicit self, used by define_method to // rebind the block's self to the method's receiver. func (vm *VM) callBlockSelf(p *Proc, self object.Value, args []object.Value) object.Value { + // ⛔ The SECOND door of #940, and the one a fix at the dispatch loop's + // backward edges cannot reach. `loop { }`, `Integer#times` and `Array#each` + // spin in a GO loop that calls this; their bytecode contains no backward + // jump, so vm.backJump never fires. Measured: wiring only the dispatch loop + // made `while true; end` agree with MRI byte for byte and left `loop { }` + // hanging exactly as before. This is the funnel every native iterator passes + // through, once per iteration. + vm.tickSafepoint() if p.native != nil { // Proc#call is nonRetaining, so the OpSend fast path hands us the caller's // live operand-stack region directly. An ISeq block body copies its args diff --git a/internal/vm/thread.go b/internal/vm/thread.go index c3d54951..7d481d36 100644 --- a/internal/vm/thread.go +++ b/internal/vm/thread.go @@ -423,6 +423,63 @@ func (vm *VM) threadBlock(fn func()) { vm.threadRelease(fn, "sleep") } // t.status != 'run'"), which never ends if a passing thread looks asleep. func (vm *VM) threadPass(fn func()) { vm.threadRelease(fn, "") } +// safepointTicks is how many units of uninterrupted Ruby progress pass between +// interpreter safepoints. It trades throughput against how long a spinning +// thread can keep the GVL: the bar #935 set for a blocked accept is that an +// interrupt arrives within 0.1 s as it does in MRI, and this is the knob that +// has to hold it for a CPU-bound thread. +// +// ⛔ The value is a starting point and NOT a measured one. A counter paid on +// every tick is only one of two designs; the other is MRI's, where the timer +// thread sets a flag and the loop pays a single load (rb_thread_schedule). +// Which one to keep is a measurement on a quiet machine, recorded on #940. +const safepointTicks = 4096 + +// tickSafepoint counts one unit of uninterrupted Ruby progress and services a +// safepoint every safepointTicks of them. +// +// ⛔ The reason this is needed is not that the existing safepoints check too +// rarely — serviceSafepoint's own comment argues correctly that checking at the +// cooperative yield points is ENOUGH, since a cross-thread raise can only be +// queued while the target holds no GVL. The hole is in the unstated premise: +// that the target eventually reaches one of those points. A bare `while true; +// end` never does. So what this adds is the YIELD that makes that premise true, +// not another check. +func (vm *VM) tickSafepoint() { + vm.sinceSafepoint++ + if vm.sinceSafepoint < safepointTicks { + return + } + vm.sinceSafepoint = 0 + vm.interpreterSafepoint() +} + +// backJump is called at every jump in the dispatch loop and does nothing unless +// the jump goes BACKWARD (target at or before the current pc), which is the only +// kind that can loop. +func (vm *VM) backJump(target, pc int) { + if target > pc { + return + } + vm.tickSafepoint() +} + +// interpreterSafepoint is the yield point a thread running pure Ruby reaches. It +// is Thread.pass's body (thread.go, Thread.pass) at a point where the +// interpreter sits between instructions, which is what makes it safe: the pc has +// already been published and no operand is half-consumed. +// +// The pass is skipped when this is the only live thread, because there is nobody +// to hand the GVL to and threadRelease would still pay an unlock, a Gosched, a +// relock and two context copies. serviceSafepoint still runs: it is three field +// reads when nothing is queued, and it is what delivers a kill or a raise. +func (vm *VM) interpreterSafepoint() { + if len(vm.threads) > 1 { + vm.threadPass(runtime.Gosched) + } + vm.serviceSafepoint(vm.currentThread) +} + // threadRelease is the body shared by threadBlock and threadPass: save this // thread's context, optionally publish a status for the window in which it does // not hold the GVL, release it, run fn, then take the GVL back and restore both. diff --git a/internal/vm/vm.go b/internal/vm/vm.go index d9f8e764..29e67815 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -770,14 +770,42 @@ type VM struct { currentFiber *Fiber // the fiber currently running (a thread's root fiber at the top level); never nil // Concurrency: an emulated GVL (one Ruby thread executes VM code at a time). - // The running goroutine holds gvl; it is released only inside blocking native + // The running goroutine holds gvl; it is released inside blocking native // methods (Thread#join, Mutex#lock, Queue#pop, sleep, Thread.pass), where the - // thread's execution context is saved and the next runnable thread's restored. + // thread's execution context is saved and the next runnable thread's restored, + // and — since #940 — at an interpreter safepoint every safepointTicks units of + // Ruby progress, which is the only release a thread running pure Ruby reaches. + // + // ⛔ This comment used to end at "only inside blocking native methods", and + // that word was the whole defect: a thread that never called one of them never + // released the GVL, so `Thread.new { while true; end }` froze the interpreter + // and Thread.new itself never returned. The list was accurate and its + // completeness was the thing nobody checked. gvl sync.Mutex currentThread *RThread // the thread holding the GVL mainThread *RThread // the root thread threads []*RThread // all live threads, for Thread.list (GVL-guarded) + // sinceSafepoint counts units of uninterrupted Ruby progress since the last + // interpreter safepoint — a backward bytecode jump, or a block invocation. + // VM.tickSafepoint services a safepoint every safepointTicks of them. + // + // ⛔ Every release site listed above is a BLOCKING call, so a thread running + // pure Ruby reached no yield point at all: `Thread.new { while true; end }` + // held the GVL for ever and Thread.new ITSELF never returned, because the + // eager-start handshake waits for the new thread to block or finish and a + // bare loop does neither (#940). + // + // ⛔ TWO kinds of progress are counted, and the second is not optional: a + // backward jump covers `while`/`until`, but `loop { }`, `Integer#times` and + // `Array#each` spin in a GO loop whose bytecode has no backward jump at all. + // Fixing only the dispatch loop was measured and left `loop { }` hanging + // exactly as before. + // + // Touched only while the GVL is held, so it needs no lock — the same reason + // envFree below needs none. + sinceSafepoint int + // envFree recycles per-call frame environments. exec checks one out at entry // and returns it at normal exit unless a closure captured it (see Env.captured // / markEnvCaptured). Touched only while the GVL is held, so it needs no lock. @@ -2586,20 +2614,29 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, subj := pop() raise("NoMatchingPatternError", "%s", subj.Inspect()) case bytecode.OpJump: + // ⛔ These four are the dispatch loop's only backward edges, and so + // the only places a thread running pure Ruby can be made to offer + // the GVL away. Before #940 none of them did, and a bare + // `loop { }` held it for ever. backJump returns at once for a + // forward jump, which is most of them. + vm.backJump(in.A, pc) pc = in.A continue case bytecode.OpBranchIf: if pop().Truthy() { + vm.backJump(in.A, pc) pc = in.A continue } case bytecode.OpBranchUnless: if !pop().Truthy() { + vm.backJump(in.A, pc) pc = in.A continue } case bytecode.OpBranchNil: if _, isNil := pop().(object.Nil); isNil { + vm.backJump(in.A, pc) pc = in.A continue } From b350493825f0c6d9d2a9c7169211c1ad41f7db27 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 15 Nov 2026 13:05:11 +0100 Subject: [PATCH 2/2] vm: test the safepoint branch the ratchet caught, which is the one the change exists for MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI on 0627f096 was RED, and the gate named it precisely: -- NEWLY BELOW 100% (1) -- internal/vm/thread.go interpreterSafepoint 66.7% (was fully covered) 2 blocks of 3, and the missing one is `vm.threadPass(runtime.Gosched)` under `if len(vm.threads) > 1` -- the branch that actually hands the GVL to another thread. The fast path (one live thread, skip the pass) was exercised by every existing test that spins a Ruby loop; the slow path, which is the whole point of the change, was not. ⛔ This was a real gap, not a coverage chore. The three-engine oracle probes on #940 did exercise it -- that is how `Thread.new { loop { } }` started working -- but those probes are not in this suite, so nothing here would have noticed the branch breaking. And my local `go test ./internal/vm` was green on exactly that gap, because the ratchet is a separate CI step: running the package's tests is not running the gate. ## The two tests TestInterpreterSafepointHandsTheGVLOverWhenAnotherThreadIsLive spins the MAIN thread through 200_000 backward jumps (~48 safepoints against safepointTicks = 4096) while a second thread is parked in sleep, which is the only shape that reaches the branch. It also asserts the peer is STILL asleep afterwards: passing the GVL to a parked thread must not wake it, run it or kill it -- a safepoint is a hand-off offer, not a resumption. TestInterpreterSafepointIsATransparentNoOpWithOneThread is the companion that makes the fast path defensible rather than merely cheap: the same loop with one thread must behave identically. ## Verified with the gate's own command, not with a passing test A passing test says nothing about coverage, so: gotest -coverprofile=... -run '' ./internal/vm go tool cover -func=... covratchet -profile ... -require interpreterSafepoint,tickSafepoint,backJump required 3 function(s), matched 3 record(s) in a profile of 7357 all required functions at 100% The denominator is printed, so a run that had read nothing could not pass for clean -- which is why `-require` exists. ## One thing I am NOT claiming The same CI run also reported three functions "further below than recorded", which that gate reports without failing on (#816): builtins.go bootstrap 99.6%, recorded 99.7% filestat.go registerFileStat 99.3%, recorded 99.4% nethttp.go registerNetHTTP 92.3%, recorded 96.0% I have NOT attributed those to this change and will not guess. Two are tenth-of-a-point drifts; the nethttp one is 3.7 points and its tests drive real servers, on a run where internal/vm took 2922 s against ~293 s locally. A timing-sensitive path taking a different branch on a very slow runner is as consistent with that as anything I did -- and saying so needs a control I have not run. Refs #940, #816 Co-Authored-By: Claude Opus 5 --- internal/vm/safepoint_test.go | 78 +++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 internal/vm/safepoint_test.go diff --git a/internal/vm/safepoint_test.go b/internal/vm/safepoint_test.go new file mode 100644 index 00000000..5dcc0f4b --- /dev/null +++ b/internal/vm/safepoint_test.go @@ -0,0 +1,78 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !rbgo_core + +package vm + +import "testing" + +// ⛔ THE BRANCH THE COVERAGE RATCHET CAUGHT, AND IT WAS WORTH CATCHING. +// +// interpreterSafepoint has a fast path: with one live thread there is nobody to +// hand the GVL to, so threadPass — an unlock, a Gosched, a relock and two +// context copies — is skipped. The gate on #942 reported +// +// internal/vm/thread.go interpreterSafepoint 66.7% (was fully covered) +// +// 2 blocks of 3, and the missing one is `vm.threadPass(runtime.Gosched)`: the +// branch that actually hands the GVL over. Every test in the suite that spins a +// Ruby loop does it with ONE thread, so the fast path was exercised and the slow +// path — the one the whole change exists for — was not. +// +// The three-engine oracle probes on #940 did exercise it (that is how +// `Thread.new { loop { } }` started working), but those are not in this suite, +// so nothing here would have noticed it breaking. A local `go test ./internal/vm` +// was green on exactly that gap, because the ratchet is a separate CI step. +// +// This test spins the MAIN thread through enough backward jumps to cross +// safepointTicks while a second thread is parked, which is the only shape that +// reaches the branch. +func TestInterpreterSafepointHandsTheGVLOverWhenAnotherThreadIsLive(t *testing.T) { + // 200_000 backward jumps against safepointTicks = 4096 is ~48 safepoints, + // every one of them with two live threads. The sleeping thread is parked, so + // it stays in vm.threads and len(vm.threads) > 1 holds throughout. + got := runSrc(t, ` +other = Thread.new { sleep 30 } +sleep 0.05 + +i = 0 +while i < 200_000 + i += 1 +end +p i + +# The peer is still alive and still asleep: passing the GVL to a parked thread +# must not wake it, run it, or kill it -- a safepoint is a hand-off offer, not a +# resumption. +p other.alive? +p other.status + +other.kill +other.join +p :joined +`) + want := "200000\ntrue\n\"sleep\"\n:joined" + if got != want { + t.Errorf("safepoint with a second live thread:\ngot %q\nwant %q", got, want) + } +} + +// And the companion property, which is what makes the fast path defensible +// rather than merely cheap: with ONE thread the same loop must behave +// identically. If the skip were wrong, this is where it would show. +func TestInterpreterSafepointIsATransparentNoOpWithOneThread(t *testing.T) { + got := runSrc(t, ` +i = 0 +while i < 200_000 + i += 1 +end +p i +p Thread.list.size +`) + want := "200000\n1" + if got != want { + t.Errorf("safepoint with one thread:\ngot %q\nwant %q", got, want) + } +}