Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion internal/vm/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 65 additions & 0 deletions internal/vm/process_exit_bang_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading