Search code examples
rubyforkminitest

Why fork exits with non-zero in Minitest test?


This is the code:

require 'minitest/autorun'
class Foo < Minitest::Test
  def test_foo
    Process.fork do
      exit(0)
    end
    p Process.waitall
  end
end

This is the output:

$ ruby a.rb
Run options: --seed 40445

# Running:

[[41827, #<Process::Status: pid 41827 exit 1>]]
.

Finished in 0.016218s, 61.6599 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

Why exit code is not zero?

The same code without Minitest works just fine:

Process.fork do
  exit(0)
end
p Process.waitall

What's wrong?


Solution

  • https://github.com/seattlerb/minitest/issues/467

    The problem is that fork duplicates the at_exit handlers of the parent process. Minitest uses at_exit to run the tests so you're hitting up against that. From the fork doco: "The child process can exit using Kernel.exit! to avoid running any at_exit functions."