Skip to content

The VM closes no IO at interpreter shutdown, where MRI closes every one #643

Closed
go-embedded-ruby/docs
#52

Description

@tannevaled

rbgo leaves every open IO/File open when the interpreter finishes. MRI closes them — ruby_finalize_1 runs rb_io_flush_all and the IO finalizers, so a script that opens a file and never closes it still releases the descriptor at exit.

How it surfaced

On the windows CI lane, three tests failed on cleanup rather than on their assertion:

--- FAIL: TestWave28FileOpenDescriptor
    testing.go:1617: TempDir RemoveAll cleanup: unlinkat C:\...\008:
    The process cannot access the file because it is being used by another process.

Those cases deliberately leave a File open — File.open(fh.fileno) needs fh alive to have a descriptor to reopen — and Windows will not delete a file with an open handle. On POSIX nobody notices, because unlinking an open file is allowed there.

So this is not a Windows-only defect. It is a VM gap that only Windows makes visible.

What it costs

  • A long-running embedder leaks descriptors for every script it runs.
  • On Windows, any file a script opened stays locked after the script ends.
  • Tests must use a best-effort scratch directory instead of t.TempDir() (see fdOpenScratch in internal/vm/wave28_file_fd_open_test.go, which says so and points here).

What it needs

A shutdown path that walks the live IOObjs and closes them, the way ruby_finalize_1 does. The VM has no registry of open streams today, so the registry is part of the work. Flushing matters as much as closing: MRI's rb_io_flush_all means buffered writes reach the file even if the script never closed it.

Found while landing #640 (File.open's mode/flags contract).

Activity

  1. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    Measured: this is silent DATA LOSS, and a discriminator says which defect it is

    f = File.open(path, "w")
    f.write("never closed")
    # no close, no flush — MRI closes every open IO at interpreter shutdown

    Run under each engine, then the file inspected from outside the process:

    file size
    MRI 4.0.7 12 bytes — never closed is on disk
    rbgo 0 bytes — the write is gone

    A Ruby program that writes a file and exits without closing it loses its data
    under rbgo. That is ordinary practice in a short script, and nothing reports it:
    the program exits 0 and the file exists, empty.

    ⛔ Which defect it is — the discriminator

    "0 bytes" has two possible causes with different fixes: the write is buffered
    and nobody flushes it, or the write never happened. Adding one line:

    f.write("never closed")
    f.flush

    → rbgo writes 12 bytes.

    So the write is real and rbgo does buffer file writes. Nothing flushes them
    at shutdown because nothing closes the IO, which is exactly what this issue's
    title says. It is not a lost write, and a fix aimed at the write path would have
    been aimed at nothing.

    ⭐ And it pairs with #676 into one gap

    Measured today on the other stream: rbgo does not buffer $stdout at all —
    no bufio anywhere, cmd/rbgo/main.go passes os.Stdout straight through —
    while $stdout.sync answers false (#676).

    stream buffered? what it reports consequence
    $stdout no sync == false the flag lies; exit! loses nothing where MRI loses the buffer
    a File opened "w" yes — nothing flushes it at shutdown; the data is lost

    Two streams, opposite behaviours, and in both cases the published or implied
    contract is the one that does not hold. The common half is that at shutdown
    nobody flushes anything
    , and MRI's rb_ec_cleanup closing every IO is what
    makes the file case work there.

    Shape of a fix, and the part that needs care

    Close every open IO at interpreter shutdown, which flushes as a side effect —
    and it has to run where at_exit runs, which means deciding what happens under
    exit!. MRI's _exit(2) skips both: handlers and the IO close. So a fix must
    NOT close on the immediate path, or it would hand exit! the flush that #676
    measures as absent in MRI.

    ⚠ That ordering is the trap: "close everything at the end" is right for exit,
    abort and falling off the end, and wrong for exit!. The test wants all four
    exits, not just the common one.

    Refs #676, #941

    🤖 Generated with Claude Code

  2. added a commit that references this issue on Nov 15, 2026
    35e63eb
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