Skip to content


eventmachine fork test failure with rubinius #2044

ttilley opened this Issue · 4 comments

4 participants


This one was fairly confusing because the individual test, when run by itself, passes just fine. The subprocess exits cleanly, the test can assert it received the text it expected from the subprocess via an IO.pipe, and all is well. However, when run after a test that calls into EM.error_handler, the forked process will not die.

This is a version of the test with just the two implicated factors: forking and EM.error_handler:

require 'em_test_helper'

class TestFork < Test::Unit::TestCase
  def test_error_handler_idempotent # issue 185
    errors = []
    ticks = []
    EM.error_handler do |e|
      errors << e
    end do
      EM.next_tick do
        ticks << :first
      EM.next_tick do
        ticks << :second
      EM.add_timer(0.001) { EM.stop }

    assert_equal 1, errors.size
    assert_equal [:first, :second], ticks

  def test_fork_safe
    return unless cpid = Process.fork { exit! } rescue false

    read, write = IO.pipe do
      cpid = Process.fork do
        write.puts "forked" do
          EM.next_tick do
            write.puts "EM ran"
    assert_equal "forked\n", read.readline
    assert_equal "EM ran\n", read.readline
    read.close rescue nil
    write.close rescue nil


If I add STDERR.puts debug statements, it does get as far as the exit! before hanging.

Due to what the error handler block actually DOES here, one could argue that the test is written poorly (and it is) to not remove the error handler as part of cleanup. In rubinius's case, that errors array is perhaps not kept alive by a reference from the block as it's passed into the EM C API?


Sure enough, adding an ensure block fixes the following test on rubinius:


I'm beginning to get curious about how this ends up working just fine on MRI18/MRI19.

Rubinius member

Seeing how this issue has been around for a while now, is there any need to keep it open? It's not entirely clear if this would be an Rbx issue, or if it has been fixed in the mean time.

Rubinius member

I added the ensure line and this test hangs. See this gist for the Ctrl+c segfaults.

Rubinius member

EM is a problematic extension due to the MRI C-API and EM's approach to working around MRI's inability to utilize parallelism. If someone wants to send a PR with a fix for this, if it's actually a Rubinius, issue, please feel free to do so.

@brixen brixen closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.