Skip to content

vm: Process.exit! skips at_exit, as Kernel#exit! already did - #947

Merged
tannevaled merged 1 commit into
mainfrom
process-exit-bang-skips-at-exit
Nov 15, 2026
Merged

tannevaled merged 1 commit into
mainfrom
process-exit-bang-skips-at-exit

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

vm: Process.exit! skips at_exit, as Kernel#exit! already did

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 noreply@anthropic.com

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 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 0fdecfc into main Nov 15, 2026
26 checks passed
@tannevaled
tannevaled deleted the process-exit-bang-skips-at-exit branch November 15, 2026 18:51
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