From ae41e9d0ad68d66c3b4f90242c78d7a119e16b50 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 15 Nov 2026 18:35:09 +0100 Subject: [PATCH] vm: Process.exit! skips at_exit, as Kernel#exit! already did MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One word, and it is a two-doors defect. MRI implements Kernel#exit! and Process.exit! with the SAME C function (rb_f_exit_bang), so they cannot differ. rbgo defines them separately: kernel_introspect.go raiseSystemExitImmediate correct process.go raiseSystemExit the non-immediate helper So Process.exit! ran the at_exit handlers whose skipping is its entire purpose. Measured against MRI 4.0.7 through cmd/oracle, on `at_exit { puts "at_exit ran" }; puts "start"; Process.exit! 4`: MRI status 4, stdout EMPTY control status 4, "start\nat_exit ran" (18 bytes) treatment status 4, "start" ( 6 bytes) ## Why nothing caught it ⛔ The EXIT STATUS was already correct through both helpers. The status is what an exit test naturally asserts, and it is identical either way: only the at_exit handlers distinguish raiseSystemExit from raiseSystemExitImmediate. A test that checks the status passes against the bug. That is also why the comment above the line now says which half each helper decides -- the `!` is the whole difference between two adjacent definitions, and nothing in the call made that visible. ## The test, and the control that makes it mean something TestProcessExitBangSkipsAtExitLikeKernelExitBang asserts three things: Process.exit! 4 -> "before" no handler exit! 4 -> "before" the door that was already right Process.exit 4 -> "before\nhandler ran" THE CONTROL ⛔ Without the third, an implementation where at_exit never ran at all would pass the first two. And the second is there so a later change cannot fix one spelling by breaking the other -- which is how this defect arose. Proved by mutation: restoring raiseSystemExit turns it red on the `Process.exit!` arm alone (`got "before\nhandler ran"`), the other two arms untouched. ## ⚠ What is deliberately NOT fixed here The treatment still prints `start` where MRI prints nothing. That is a separate defect and it is named rather than folded in: rbgo does not buffer $stdout at all -- no bufio anywhere, cmd/rbgo/main.go passes os.Stdout straight through -- while reporting `$stdout.sync == false`. So there is no buffer for exit! to discard. #676 carries that measurement, including a correction to my own first diagnosis of it: I had said "MRI discards the buffer and rbgo flushes it" and offered `$stdout.sync = true` making both agree as proof. The two engines agreed for DIFFERENT reasons -- sync made MRI's write immediate, and rbgo's always were -- so that experiment had no treatment arm on the rbgo side and proved nothing. The choice between buffering as MRI does and reporting sync honestly is on #676 and is not this change's to make. ## Checks - `gotest` over the whole exit/at_exit family: TestProcessExitFamilyIsPublic, TestExitBangSkipsAtExitHandlers, TestAtExitExitOverridesTheStatus, TestForkExitStopsTheChildNotTheProgram, TestSystemExitFamily, TestKernelExit, TestRunAtExitOneRepanic, TestAtExitErrorSwallowed, TestSimpleCovAtExit and the new one -- all green. - `gofmt -l internal/vm` silent. Refs #676 Co-Authored-By: Claude Opus 5 --- internal/vm/process.go | 23 +++++++++- internal/vm/process_exit_bang_test.go | 65 +++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 internal/vm/process_exit_bang_test.go diff --git a/internal/vm/process.go b/internal/vm/process.go index 8c222d0b..560ed36b 100644 --- a/internal/vm/process.go +++ b/internal/vm/process.go @@ -345,8 +345,29 @@ func (vm *VM) defineProcessExit(def func(string, NativeFn)) { def("exit", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { return vm.raiseSystemExit(vm.exitStatusArg(args, 0), "exit") }) + // ⛔ raiseSystemExitImmediate, not raiseSystemExit: the ! is the whole + // difference between these two definitions, and this door had the + // non-immediate helper. Kernel#exit! (kernel_introspect.go) got it right and + // Process.exit! did not, so the two spellings of one method behaved + // differently — MRI implements both with rb_f_exit_bang. + // + // Measured 2026-11-15 against MRI 4.0.7 through cmd/oracle, on + // `at_exit { puts "at_exit ran" }; puts "start"; Process.exit! 4`: + // + // MRI status 4, stdout EMPTY (no at_exit, nothing flushed) + // rbgo status 4, "start\nat_exit ran" + // + // The status was already right, which is why nothing caught this: only the + // at_exit handlers tell the two helpers apart, and a test that checks the + // status alone passes either way. + // + // ⚠ The `start` half of that output is a SEPARATE defect and is not fixed + // here: rbgo does not buffer $stdout at all (no bufio anywhere; + // cmd/rbgo/main.go passes os.Stdout straight through) while reporting + // `$stdout.sync == false`. See #676 — the choice between buffering as MRI + // does and reporting sync honestly is not this change's to make. def("exit!", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - return vm.raiseSystemExit(vm.exitStatusArg(args, 1), "exit") + return vm.raiseSystemExitImmediate(vm.exitStatusArg(args, 1), "exit") }) def("abort", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { return vm.send(vm.main, "abort", args, blk) diff --git a/internal/vm/process_exit_bang_test.go b/internal/vm/process_exit_bang_test.go new file mode 100644 index 00000000..2b72728a --- /dev/null +++ b/internal/vm/process_exit_bang_test.go @@ -0,0 +1,65 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !rbgo_core + +package vm + +import "testing" + +// ⛔ ONE METHOD, TWO SPELLINGS, TWO BEHAVIOURS. +// +// MRI implements Kernel#exit! and Process.exit! with the same C function +// (rb_f_exit_bang), so they cannot differ. rbgo defined them separately: +// kernel_introspect.go reached raiseSystemExitImmediate and process.go reached +// raiseSystemExit — the non-immediate helper — so Process.exit! ran the at_exit +// handlers that its whole point is to skip. +// +// Measured against MRI 4.0.7 through cmd/oracle before the fix: +// +// at_exit { puts "at_exit ran" }; puts "start"; Process.exit! 4 +// MRI status 4, stdout EMPTY +// rbgo status 4, "start\nat_exit ran" +// +// ⛔ The EXIT STATUS was already correct, which is why nothing caught it: the +// status is what an exit test naturally asserts, and it is identical through +// both helpers. Only at_exit distinguishes them. So this test asserts the +// handler, and uses `exit` as its own control — a test that only checked +// `exit!` could pass against an implementation where NEITHER runs handlers. +func TestProcessExitBangSkipsAtExitLikeKernelExitBang(t *testing.T) { + // Process.exit! — no handler. + got := runSrc(t, ` +at_exit { puts "handler ran" } +puts "before" +Process.exit! 4 +puts "unreachable" +`) + if got != "before" { + t.Errorf("Process.exit! should skip at_exit:\ngot %q\nwant %q", got, "before") + } + + // Kernel#exit! — the spelling that was already right, asserted so a future + // change cannot fix one door by breaking the other. + got = runSrc(t, ` +at_exit { puts "handler ran" } +puts "before" +exit! 4 +puts "unreachable" +`) + if got != "before" { + t.Errorf("Kernel#exit! should skip at_exit:\ngot %q\nwant %q", got, "before") + } + + // ⛔ THE CONTROL. Without it, an implementation where at_exit never runs at + // all would pass both assertions above. + got = runSrc(t, ` +at_exit { puts "handler ran" } +puts "before" +Process.exit 4 +puts "unreachable" +`) + if got != "before\nhandler ran" { + t.Errorf("Process.exit should RUN at_exit:\ngot %q\nwant %q", got, "before\nhandler ran") + } +}