Skip to content

A deliberate exit status never reaches the process: rbgo -e 'exit 3' exits 0 #676

Open

Description

@tannevaled

rbgo -e 'exit 3' exits 0, where MRI exits 3. Every spelling of a
deliberate non-zero exit is lost, so rbgo cannot signal failure to a shell,
a Makefile or a CI step.

Measured against MRI 4.0.5 (ruby 4.0.5 (2026-05-20) +PRISM [arm64-darwin25])
on the branch of #675:

program MRI status rbgo status
exit 3 3 0
exit false 1 0
exit!(4) 4 0
abort "boom" 1 0
raise "x" 1 1 (correct)
exit 0 0 0 (correct)
Process.exit(5) 5 1, NoMethodError (see below)
Process.exit!(6) 6 1, NoMethodError (see below)

The VM half is already correct — only the front-end's last step is missing

This is not a SystemExit implementation gap. Everything inside the VM behaves
as MRI does:

$ rbgo -e 'begin; exit 3; rescue SystemExit => e; puts "status=#{e.status} success=#{e.success?}"; end'
status=3 success=false          # identical to MRI
$ rbgo -e 'at_exit { puts "atexit ran" }; exit 3'
atexit ran                      # identical to MRI; only the status differs
$ rbgo -e 'begin; exit 3; ensure; puts "ensure ran"; end'
ensure ran                      # identical to MRI; only the status differs

SystemExit is raised, carries the right status and success?, is rescuable,
and at_exit and ensure both run. The defect is at exactly one boundary: the
CLI never translates a terminal SystemExit into the process's exit code. MRI
does this in main() → ruby_run_node → ruby_cleanup, which calls
error_handle and returns ex from the SystemExit's status
(eval.c/ruby.c, tag v3_4_0).

abort follows from the same cause: rb_f_abort raises SystemExit with
status 1 (process.c).

Second, separate defect: Process.exit is private

MRI declares exit, exit! and abort as module functions on Process
(Init_process: rb_define_module_function(rb_mProcess, "exit", ...)), so
Process.exit(5) is public. rbgo only has the private Kernel copies, so the
qualified spelling raises NoMethodError. Worth fixing together, but it is a
different cause from the status loss.

What this does NOT affect

The ruby/spec ratchet is unaffected: scripts/conformance/rubyspec/run.sh
scores each file by parsing the RBGO_RESULT pass=… fail=… line the shim
prints, not by reading the child's exit status. I checked this before filing,
because a harness that read the status would have been scoring every file as a
success. Every conformance total measured so far stands.

Found while verifying which stream Kernel#abort's message reaches, in #667.

Activity

  1. tannevaled commented on Sep 26, 2026

    @tannevaled
    ContributorAuthor

    Correction to this issue's central claim, which was mine and was wrong.

    I wrote: "This is not a SystemExit implementation gap. Everything inside the VM
    behaves as MRI does … The defect is at exactly one boundary: the CLI never
    translates a terminal SystemExit into the process's exit code."

    That localisation is false. internal/vm/vm.go:1131 deliberately discards it,
    and says so:

    // A SystemExit (Kernel#exit/abort) is a clean program termination, not a
    // crash: unwind to the top, run at_exit handlers, and return without an
    // error — so a real CLI that ends by calling exit (e.g. Puppet's
    // exit_on_fail) terminates quietly rather than printing a backtrace.
    if vm.isSystemExit(rerr.Class) {

    So no front-end change alone could have fixed it. The status was never handed out
    of the VM to be translated.

    Why I got it wrong, since the reasoning error is the reusable part. I probed
    what Ruby code can observe: rescue SystemExit sees the right status, at_exit
    runs, ensure runs. All three are true. I inferred "everything inside is correct,
    therefore only the last step outside is missing" — and that does not follow. Those
    probes establish that the exception is raised and unwinds correctly; they say
    nothing about whether its value survives the VM's own boundary. Observability
    from inside the language is not evidence about where a value is dropped on the way
    out.

    The comment above is also worth reading as its own lesson: the discard was a
    correct decision for the problem it was written for (a CLI ending in exit should
    not print a backtrace) and became wrong when the exit status started to matter.
    The premise moved; the code did not.

    Fixed in #693, which had to touch vm.go for exactly this reason and flagged it as
    outside its granted file set.

    Two further corrections from that work, both to figures in this issue's neighbour
    #691:

    • the rate is 10–20% (3/20, 3/20, 2/20 across spellings), not the 2-in-3 I
      quoted from three runs;
    • Process.exit being private was right, and the fix makes all nine spellings
      byte-identical to MRI 4.0.5 across 20 runs each.
  2. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    Refuted as written — exit 3 reaches the process. But a different divergence lives in the same family.

    Measured 2026-11-15 through cmd/oracle against MRI 4.0.7, rbgo built from
    main. The oracle reports the exit status as a number, which is this issue's
    whole question.

    The three forms that agree

    program MRI 4.0.7 rbgo
    puts "start"; exit 3 status 3, at_exit runs identical, byte for byte
    abort "aborted" status 1, message on stderr, at_exit runs identical, byte for byte
    exit false status 1, at_exit runs identical, byte for byte

    And directly on the form the title names:

    $ rbgo -e 'puts "before"; exit 3; puts "after"'
    before
    $ echo $?
    3
    

    So "rbgo -e 'exit 3' exits 0" does not reproduce. I cannot say when it stopped
    reproducing — this is a measurement of the present, not a bisect — so this is a
    candidate for closing as not reproducible rather than as completed.

    ⛔ exit! does diverge, and it is a different defect

    at_exit { puts "at_exit ran" }
    puts "start"
    exit! 4
    
    MRI 4.0.7 rbgo
    exit status 4 4
    stdout 0 bytes 6 bytes (start)

    exit! skips at_exit in both — correct. The difference is that MRI discards
    the buffered stdout
    and rbgo flushes it.

    ⭐ Verified rather than assumed: adding $stdout.sync = true makes both print
    start and agree byte for byte. That rules out an ordering difference and
    isolates the flush — rbgo writes what MRI throws away.

    rbgo is the "nicer" of the two here and that is exactly why it is wrong:
    exit!'s whole purpose is leaving without flushing, which is why it is what a
    forked child calls. The consequences inside a forked block are measured on #774,
    because they belong to fork rather than to the exit status this issue is about.

    What a fix would need to hold

    • exit! leaves without flushing $stdout/$stderr.
    • exit, abort and exit false keep flushing and keep running at_exit —
      all three are correct today and a change to the exit path must not move them.
    • The regression test wants $stdout.sync false (the default for a pipe):
      with sync on, the two engines agree and the test would pass either way. That
      is the property that makes this easy to "fix" invisibly.

    Refs #774, #941

    🤖 Generated with Claude Code

  3. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    ⛔ Correction to my comment above: the diagnosis was wrong, and so was the "verification" I offered for it

    I wrote that MRI discards buffered stdout at exit! and rbgo flushes
    it, and offered this as proof:

    ⭐ Verified rather than assumed: adding $stdout.sync = true makes both print
    start and agree byte for byte. That rules out an ordering difference and
    isolates the flush.

    Both halves are wrong. Read in the source:

    $ git grep -n "bufio.NewWriter" -- internal/vm/io.go cmd/rbgo interp.go run.go
    (no match)
    
    cmd/rbgo/main.go:125:  machine := vm.NewWithStderr(os.Stdout, os.Stderr)
    

    rbgo does not buffer stdout at all. os.Stdout is passed straight through,
    and interp.go's switchWriter.Write is a one-line pass-through. Every puts
    reaches the file descriptor as it is called.

    So there is no buffer for exit! to discard, and "rbgo flushes where MRI
    discards" describes a mechanism rbgo does not have.

    ⛔ And the verification was a proxy where two errors cancelled

    With $stdout.sync = true the two engines agreed — for different reasons:

    why it printed start
    MRI sync made the write immediate, so it was already out
    rbgo it was always writing immediately; sync changed nothing

    Agreement produced by two different mechanisms is not evidence about either.
    I presented it as isolating the flush, and it could not have: setting sync is
    a no-op in rbgo, so that experiment had no treatment arm on the rbgo side.

    What the defect actually is

    p $stdout.sync   # MRI: false     rbgo: false
    

    The flag agrees with MRI and the behaviour does not: rbgo answers false
    while writing through. So the real divergence is one layer down from where I put
    it, and the exit! difference is a symptom of it rather than a defect of its
    own.

    That changes the fix from "do not flush on exit!" to a choice:

    1. Buffer $stdout as MRI does when it is not a tty, and let sync,
      flush, exit! and a hard kill all mean what they mean in MRI. This is the
      faithful option and the largest: it puts a buffer in front of every write and
      makes several other behaviours (interleaving with $stderr, output ordering
      across a system call, what survives a crash) depend on getting it right.
    2. Report sync honestly — answer true, since that is what the writes
      do — and accept that $stdout.sync = false cannot be honoured. Cheap, and it
      stops the flag lying, but it diverges from MRI's default value, which a spec
      may assert.

    I am not choosing between them: option 1 touches output ordering everywhere and
    option 2 publishes a different default than MRI's. Both are maintainer calls.

    What stands from the comment above

    The three agreements and the refutation are unaffected — they are exit statuses
    and at_exit, measured directly:

    • exit 3, abort, exit false: byte for byte with MRI. This issue as
      titled does not reproduce.
    • The exit! output difference is real as an OBSERVATION; only my explanation
      of it was wrong.

    And the Process.exit! finding below is unaffected and is a separate, clean
    defect.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions