Skip to content

vm: a thread dying of an exception reports it, and Thread#to_s has its line - #986

Merged
tannevaled merged 3 commits into
mainfrom
thread-report-on-exception
Nov 29, 2026
Merged

tannevaled merged 3 commits into
mainfrom
thread-report-on-exception

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

A thread that ends on an unhandled exception while report_on_exception is true now reports it, as MRI 4.0.10 does. Before this change rbgo printed nothing; the accessors existed, but a comment said the report was deliberately not printed.

#<Thread:0x0000000120347e68 prog.rb:3 run> terminated with exception (report_on_exception is true):
prog.rb:3:in 'block in <main>': a1 (ArgumentError)

Thread#to_s / #inspect also printed FILE:0 and an unpadded address. They now print the block's first line and a 16-digit address.

How

  • reportThreadException follows thread_start_func_2 (thread.c v4.0.10:726-739):
    • it writes Thread#to_s + " terminated with exception (report_on_exception is true):\n" through $stderr#write, then Exception#full_message (the Ruby face of rb_ec_error_print);
    • it runs before the thread is marked dead, so the status reads run;
    • a SystemExit is not reported;
    • the per-thread flag and the inherited class default both silence it;
    • a failure while printing is swallowed, as rb_ec_error_print does.
  • describe takes its line from rb_proc_location of the block (FirstLine), as rb_thread_to_s does.
  • Tests whose subject is not the report (mutex release, abort_on_exception, the thread table, one wave-24 case) now set Thread.report_on_exception = false, as ruby/spec's thread fixtures do. MRI prints the report for those programs too.

Evidence

  • rubydiff -a rbgo -b ruby 4.0.10 on the probe program, run directly and through require:
    • this branch: AGREE;
    • origin/main (ed535866): DIVERGE, printing nothing for either thread.
  • The SystemExit probe: AGREE.
  • TestThreadReportOnException and TestThreadReportSkipsSystemExit (new) use MRI 4.0.10's output.
  • 56 Thread/Abort/Wave24/Mutex tests pass locally.
  • mutsweep: both new guards are caught (one by an assertion; the other by the nil dereference it prevents).
  • gofmt -l: clean.

Not in this PR (found while measuring)

The report body still differs from MRI in three ways, each a backtrace defect outside this change:

  1. A thread's backtrace carries the spawning frame (from prog.rb:3:in '<main>'), which MRI's thread backtrace does not.
  2. The Array#each frame of a native iterator is missing.
  3. A required file's top level is labelled '<main>' where MRI says '<top (required)>'.

MRI also prints error_highlight's snippet under the message, and rbgo has no error_highlight. The new test masks the label, so it asserts only what this change owns.

rbgo starts a thread eagerly, so the report can appear before the creating thread's next line where MRI shows it later. That is a scheduling difference, not part of this change.

🤖 Generated with Claude Code

tannevaled and others added 3 commits November 29, 2026 18:25
…s line

A thread that ended on an unhandled exception while report_on_exception
was true printed nothing; MRI writes "<Thread#to_s> terminated with
exception (report_on_exception is true):" and the error through
$stderr#write, before the thread is marked dead (thread.c
thread_start_func_2, v4.0.10:733-739). A SystemExit is not reported, and
the per-thread flag and the inherited class-level default both silence
it. The accessors were already honoured; the "rbgo does not print"
comment is gone with the gap.

Thread#to_s printed "FILE:0" and an unpadded address. The line is now
the block's first line (rb_thread_to_s appends rb_proc_location) and the
address is 16 hex digits, as rbgo's other identity reprs are.

Tests whose subject is not the report now set
Thread.report_on_exception = false, as ruby/spec's thread fixtures do:
MRI would print the report for those programs too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With report_on_exception true by default, three cases of
TestBlockingWaitNotInterruptedWhenItShouldNotBe received the report MRI
writes to $stderr for a thread that dies with an exception: the harness
gives the VM one writer for both streams, so it landed in the compared
output on every CI lane. Those cases test that a wait is not ended, not
the report, so each sets Thread.report_on_exception = false first, as a
Ruby test of the same thing would.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
@tannevaled
tannevaled force-pushed the thread-report-on-exception branch from 57167f2 to d3fc22e Compare November 29, 2026 17:26
@tannevaled
tannevaled merged commit 12b6bce into main Nov 29, 2026
26 checks passed
@tannevaled
tannevaled deleted the thread-report-on-exception branch November 29, 2026 20:09
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