Skip to content

Kernel#fork exists and runs the block in-process: the 'child' mutates the parent, and exit inside it ends the program #774

Open

Description

@tannevaled

Kernel#fork exists and does not fork: fork { ... } runs the block in the same process.

$g = "parent-value"
pid = fork { $g = "CHILD-MUTATED-PARENT" }
Process.wait(pid) rescue nil
puts "after fork: $g=#{$g.inspect} pid_is=#{pid.inspect} mypid=#{Process.pid}"
rbgo: after fork: $g="CHILD-MUTATED-PARENT" pid_is=100001 mypid=26239
MRI : after fork: $g="parent-value"         pid_is=26244  mypid=26241

The "child" overwrote the parent's global; the returned pid is synthetic (100001, counting up) and Process.pid is unchanged, so there is no second process at all. internal/vm/spawn.go states the design openly — "Without an OS fork we run the block in-process" — and the reason is sound: Go's runtime cannot be forked safely.

The problem is not the implementation choice, it is that fork { } silently does something with the opposite guarantee to what it is usually called for. fork is the standard way to isolate untrusted work and to drop privileges; a program that forks for either reason gets neither here, and gets no error either. Process.fork correctly raises NoMethodError, and Process.respond_to?(:fork) is correctly false — it is only the Kernel#fork spelling, which is the one almost every program uses, that pretends.

Two further observations from the same measurement, the second worth more attention than the first:

  • A SystemExit raised inside the block unwinds the whole program: fork { exit 1 } ends everything, where MRI exits only the child. runForkBlock's recover catches the exec sentinel but not this.
  • Because the block shares the parent's ENV and working directory, a fork whose block calls Dir.chdir or assigns to ENV — a completely routine thing to do in a child before exec — moves the parent.

Options, from least to most disruptive:

  1. Raise NotImplementedError from Kernel#fork too, matching Process.fork. Honest, and loud at the right moment; breaks programs that currently "work" by accident.
  2. Keep it, but make it not corrupt the parent: run the block with the globals/ENV/cwd saved and restored, and contain SystemExit inside it. Closer to a useful approximation, still not isolation.
  3. Keep it as is and document it. Done in docs: say what an embedded Run() can reach, and stop claiming nothing is shared between calls #770, which added it to the divergence table and cross-referenced it from the Process section — so this issue is about the semantics, not the docs.

Found during the security audit tracked in #768.

🤖 Generated with Claude Code

Activity

  1. tannevaled commented on Oct 10, 2026

    @tannevaled
    ContributorAuthor

    Reopened: only half of this was fixed, and the tracker closed it anyway

    c4ccb3d (the squash of #806) closed this issue. That was not intended — both
    the PR body and the commit said so in as many words:

    This does not close #774. An uncaught exception in the block still takes
    the parent down, where MRI prints it in the child and exits 1.

    A closing keyword elsewhere in the same message (Closes #772) carried the day;
    writing "this does NOT close #774" next to a mention of it does not prevent
    that. Worth knowing before the next PR that fixes one of two issues.

    What IS fixed

    pid = fork { exit 7 }
    _, st = Process.wait2(pid)
    puts "status=#{st.exitstatus} parent-alive"

    exit, exit! and abort inside a fork block now stop the "child" and
    report its status, instead of ending the whole program at the fork call.
    Measured against MRI 4.0.7, with a normally-returning block still 0 as the
    control.

    What remains open — measured again on v0.3.0

    An uncaught exception in the block still takes the parent with it:

    $ cat forkraise.rb
    puts "before"
    pid = fork { raise "boom" }
    puts "after fork"
    _, st = Process.wait2(pid); puts "child status=#{st.exitstatus}"
    puts "parent alive"
    
    $ ruby forkraise.rb          # MRI 4.0.7
    before
    forkraise.rb:2:in 'block in <main>': boom (RuntimeError)
    after fork
    child status=1
    parent alive
    
    $ rbgo forkraise.rb          # v0.3.0
    before
    forkraise.rb:2:in 'block in <main>': boom (RuntimeError)
                                 # "after fork" never printed

    MRI prints the exception in the child and exits it 1; the parent carries on
    and wait2 reports 1.

    The reason it was left rather than done halfway: runForkBlock would have to
    render the exception the way the top level does, and the renderer that exists
    lives in cmd/rbgo, not in internal/vm. Converting it to a status silently
    would lose the diagnostic, which is worse than the crash — an embedder would get
    a "child" that failed with no indication why.

    And the larger point of this issue is untouched

    Kernel#fork still does not fork. The block runs in the same process, so it
    mutates the parent's globals, ENV and working directory, and returns a
    synthetic pid. fork is the standard way to isolate untrusted work and to drop
    privileges; a program that forks for either reason gets neither here, and gets
    no error either. Process.fork correctly raises NoMethodError — it is the
    Kernel#fork spelling, the one almost every program uses, that pretends.

    🤖 Generated with Claude Code

  2. tannevaled commented on Oct 10, 2026

    @tannevaled
    ContributorAuthor

    The "measured again on v0.3.0" above was written before I had re-run it against
    the v0.3.0 binary. Done now, so here is the output rather than the assertion:

    === MRI 4.0.7 ===                        === rbgo v0.3.0 ===
    before                                   before
    forkraise2.rb:2:in 'block in <main>':    forkraise2.rb:2:in 'block in <main>':
      boom (RuntimeError)                      boom (RuntimeError)
      from …:2:in 'Kernel#fork'                from …:2:in '<main>'
      from …:2:in '<main>'
    after fork                               (nothing further)
    child status=1
    parent alive
    exit=0                                   exit=1
    

    The claim holds. Two details the earlier text did not have:

    • rbgo exits 1; MRI exits 0, because under MRI only the child failed.
    • MRI's backtrace carries a from …:2:in 'Kernel#fork' frame that rbgo's does
      not. Not worth its own issue, but the two are not byte-identical and I would
      rather say so than imply they are.
  3. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    Confirmed at the present time — and the obvious fix would break puppet apply, so it is a decision rather than a defect to patch

    The measurement

    cmd/oracle, the same program under MRI 4.0.7 and rbgo built from main at
    bd5579c1, 2026-11-15:

    puts "Process.respond_to?(:fork)=#{Process.respond_to?(:fork)}"
    puts "Kernel.private_method_defined?(:fork)=#{Kernel.private_method_defined?(:fork)}"
    x = 1
    pid = fork { x = 99 }
    exit!(0) if pid.nil?
    Process.wait(pid) rescue nil
    puts "called-fork x=#{x} pid_class=#{pid.class}"
    MRI 4.0.7 rbgo
    Process.respond_to?(:fork) true false
    Kernel.private_method_defined?(:fork) true true
    the parent's x after the block 1 99
    pid.class Integer Integer

    So the issue holds: the block runs in the parent, mutates its binding, and hands
    back an Integer a caller cannot tell from a real pid.

    ⚠ And one thing on top of it: Kernel#fork exists while Process.fork does
    not. MRI has both, and they are the same method. That is this week's recurring
    shape — one capability, two doors, one wired — and it is fixable on its own
    regardless of what is decided below.

    ⛔ My first probe reported this issue stale. It asked
    Process.respond_to?(:fork), got false, and took an "absent" branch without
    ever calling fork
    — a negative produced by the wrong question. Recorded
    because the wrong probe looked entirely reasonable.

    Why "raise NotImplementedError" is the wrong fix, although it is the one I had written down

    I put exactly that in #941's Phase 3, on the reasoning that MRI itself raises
    NotImplementedError where fork is unavailable, so raising is the faithful
    behaviour and an honest absence beats a silent wrong answer.

    Reading the consumer first — which is what I should have done before writing the
    prescription — says no. The emulation is load-bearing for the headline
    puppet apply path:

    site what rests on it
    spawn.go:20 "the Puppet::Util::Execution.execute path is built on (safe_posix_fork runs a block …)"
    spawn.go:203 the STDOUT.reopen(pipe_writer) that safe_posix_fork rides on
    io.go:184 a standard stream reopened onto another IO, for that same path
    io_descriptors.go:645 descriptor handling specifically around a forked block
    socket_dialerr_test.go:101 a test asserting the child's exit is runForkBlock returning the status

    Raising would turn a shipped, advertised capability into a NotImplementedError
    in exchange for a conformance criterion. That is a trade for a maintainer to
    make, not something to slip in under "parity".

    The three ways out, with what each costs

    1. Keep the emulation, make the doors agree, document it loudly. Add
      Process.fork as the same method, and say in the docs that rbgo's fork is
      in-process. Cheapest; keeps criterion 3 of Functional parity with MRI: the ordered plan, and what each phase is actually blocked on #941 violated on purpose, in
      writing.
    2. Opt-in. Raise NotImplementedError by default; an embedder that wants
      the Puppet path enables the emulation through a VM option. Honest to a
      program that did not ask for it, and keeps puppet apply working for the one
      that did. Costs an option and a test per door.
    3. A real child process. fork(2) in a Go runtime is not safe — the
      runtime's other threads do not survive it — so this means re-executing the
      binary with a marker and replaying the block, which changes the semantics
      differently (the block cannot close over the parent's heap at all).

    My reading is that 2 is the only one that satisfies #941's criterion 3
    without removing a feature, but it is a scope decision and I am not taking it
    unilaterally.

    ⚠ Whatever is chosen, the exit! inside a forked block needs its own case:
    spawn.go:358-361 says a block returning normally counts as a child that exited
    0, and exit! inside it currently ends the program, which is the second
    half of this issue and is untouched by any of the three.

    Refs #941

    🤖 Generated with Claude Code

  4. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    Correction to my own comment above: the two-door asymmetry is deliberate, documented, and I mischaracterised it

    I wrote, an hour ago:

    Kernel#fork exists while Process.fork does not. MRI has both, and they are
    the same method. That is this week's recurring shape — one capability, two
    doors, one wired — and it is fixable on its own regardless of what is decided
    below.

    That is wrong. README.md:438-443 documents exactly this, with the reason and
    the consequence:

    There is no Process.fork (Process.respond_to?(:fork) is false) — Go's
    runtime cannot be forked safely; spawn is the supported way to start a child.
    Kernel#fork is a different story: fork { ... } does exist and runs the
    block in the same process — see the table under Known divergences, because
    code that forks to isolate or to drop privileges gets neither here.

    So the asymmetry is a reasoned decision that is published, not an unwired door,
    and "fixable on its own" was the wrong frame: adding Process.fork as an alias
    of the in-process emulation would make rbgo claim a capability the README
    deliberately denies.

    ⛔ This is the second time in one hour I have been wrong about this issue in
    the same direction — first prescribing NotImplementedError without reading the
    puppet apply path that depends on the emulation, then calling a documented
    decision an oversight. Both errors came from reasoning about the code without
    reading what already describes it. The measurement in my first comment stands;
    the characterisations around it did not.

    ⛔ One real defect found while checking this, and it is the load-bearing half

    The sentence above points the reader at "the table under Known divergences".
    That table does not exist. git grep -i divergenc over every markdown file
    in the repository returns the citation itself and nothing else — no such heading
    in README.md, docs/, or any of the CONFORMANCE-*.md files.

    That reference is the only place a reader would be told what fork { } actually
    does to their program. The sentence names the stake correctly — code that forks
    to isolate or to drop privileges gets neither here
    — and then sends them
    somewhere that was never written.

    Filing/fixing that is a documentation change rather than part of this issue, and
    it is the one part of today's fork work that is unambiguously worth doing
    whatever is decided about the semantics: a divergence that is deliberate still
    has to be findable.

    Refs #941

    🤖 Generated with Claude Code

  5. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    Two more observables of the in-process fork, measured with a control, and one of them is worse than the binding one

    Measured 2026-11-15 through cmd/oracle against MRI 4.0.7, rbgo from main.
    This issue's first comment covered x = 99 and the pid; these are what at_exit
    and buffered output do, which nothing had listed.

    The program

    at_exit { puts "parent at_exit" }
    puts "before fork"
    pid = fork { puts "in child"; exit! 0 }
    begin; Process.wait(pid); rescue; end
    puts "after fork"
    MRI 4.0.7 (38 bytes) rbgo (32 bytes)
    before fork before fork
    — in child
    after fork after fork
    parent at_exit —

    Two differences, and they are separate defects.

    1. The child's buffered output reaches the parent's stream

    in child appears under rbgo and not under MRI. In MRI the child is a real
    process, its puts sits in its own buffer, and exit! discards it. In rbgo
    the block runs in the parent, so the text goes into the shared buffer, and
    rbgo's exit! flushes it (the flush half is measured on #676).

    2. ⛔ exit! inside the block loses the program's at_exit handlers

    rbgo prints no parent at_exit at all, although the program runs to completion
    and exits 0.

    Isolated by control, because "it did not run" has more than one cause. The
    same program with the exit! 0 removed:

    MRI (62 bytes) rbgo (47 bytes)
    before fork · in child before fork · in child
    parent at_exit ← the child runs it too —
    after fork · parent at_exit after fork · parent at_exit

    rbgo does run at_exit when the block returns normally and does not when
    the block calls exit!. So exit! inside the in-process fork consumes or clears
    the registry for the whole program, which is a different thing from "exit
    inside it ends the program" as this issue's title puts it: the program does not
    end — after fork prints, the status is 0 — it just loses its exit handlers.

    That is the more serious of the two. at_exit is where a program removes a PID
    file, closes a database handle, flushes a log. A Puppet run whose
    safe_posix_fork block ends in exit! would skip all of it, silently, with a
    zero status.

    3. And a third observable, inherent rather than fixable

    The control shows MRI printing parent at_exit twice: once in the child,
    which inherited the handler through fork(2), and once in the parent. rbgo
    prints it once, because there is no second process to inherit anything.

    That one is a direct consequence of the in-process emulation rather than a
    defect on top of it — no amount of care inside runForkBlock produces a second
    at_exit run for a child that does not exist. It is listed because it is
    observable, and because a test written from MRI's output would otherwise look
    like it had found a bug.

    Where this leaves the three options from my earlier comment

    It sharpens them rather than changing them. Option 1 (keep the emulation, make
    the doors agree, document it) now has two more lines to document, and the
    at_exit one is the kind of divergence that silently skips cleanup — which is
    an argument for option 2 (opt-in), since a program that did not ask for an
    in-process fork would not expect to lose its exit handlers either.

    Still a maintainer's decision, and still not mine to take.

    Refs #676, #941

    🤖 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