Skip to content

Commit d3fc22e

Browse files
tannevaledclaude
andcommitted
vm: test the thread report's failed-write branches
The ubuntu coverage ratchet put reportThreadException at 83.3%: nothing reached the recover that keeps a failing $stderr#write from becoming a second failure, nor the re-panic that keeps a Go-level panic visible. Two internal tests drive each through a $stderr whose #write raises a Ruby error, then panics in Go. MRI 4.0.10 does not return from the first case: a thread dying while $stderr#write raises hangs for the whole 30 s rubydiff budget. rbgo swallows the write failure as rb_ec_error_print's EC_PUSH_TAG intends, and the test says so instead of claiming parity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3986517 commit d3fc22e

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) the go-embedded-ruby/ruby authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
package vm
6+
7+
import (
8+
"io"
9+
"testing"
10+
11+
"github.com/go-embedded-ruby/ruby/internal/object"
12+
)
13+
14+
// reportWithSink runs reportThreadException for a thread that died of a
15+
// RuntimeError, with $stderr set to an object whose #write is write.
16+
func reportWithSink(t *testing.T, write func()) {
17+
t.Helper()
18+
vm := New(io.Discard)
19+
cls := newClass("ReportSink", vm.cObject)
20+
cls.define("write", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value {
21+
write()
22+
return object.NilV
23+
})
24+
vm.globals["$stderr"] = &RObject{class: cls, ivars: map[string]object.Value{}}
25+
exc := vm.send(vm.consts["RuntimeError"], "new", []object.Value{object.NewString("x")}, nil)
26+
th := &RThread{err: &RubyError{Class: "RuntimeError", Obj: exc}, reportOnException: true}
27+
vm.reportThreadException(th)
28+
}
29+
30+
// TestReportThreadExceptionSwallowsARubyError: a $stderr whose #write raises
31+
// must not turn the report into a second failure, as rb_ec_error_print runs
32+
// under EC_PUSH_TAG. (MRI 4.0.10 itself never returns from this case: the
33+
// probe hangs for 30 s, so the behaviour asserted here is rbgo's, not a copy.)
34+
func TestReportThreadExceptionSwallowsARubyError(t *testing.T) {
35+
calls := 0
36+
reportWithSink(t, func() {
37+
calls++
38+
raise("RuntimeError", "boom")
39+
})
40+
if calls != 1 {
41+
t.Errorf("the report must stop at the first failing write, wrote %d times", calls)
42+
}
43+
}
44+
45+
// TestReportThreadExceptionKeepsAGoPanic: a Go-level panic while printing is
46+
// an rbgo defect, not a Ruby exception, and must not be swallowed.
47+
func TestReportThreadExceptionKeepsAGoPanic(t *testing.T) {
48+
defer func() {
49+
if r := recover(); r != "plain go panic" {
50+
t.Errorf("a Go panic while reporting must propagate, got %v", r)
51+
}
52+
}()
53+
reportWithSink(t, func() { panic("plain go panic") })
54+
t.Fatal("expected the Go panic to propagate")
55+
}

0 commit comments

Comments
 (0)