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/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) + } +} 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 }