Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a test and @tmm1s patch for making post-fork EM.run calls work as…

… expected.

Closes #213
  • Loading branch information...
commit 427a76d270f796688bdeaa0bbe5c71414101cdbe 1 parent 8cd81fb
James Tucker raggi authored
Showing with 33 additions and 2 deletions.
  1. +9 −2 lib/eventmachine.rb
  2. +24 −0 tests/test_basic.rb
11 lib/eventmachine.rb
View
@@ -155,7 +155,13 @@ def self.run blk=nil, tail=nil, &block
# will start without release_machine being called and will immediately throw
#
-
+ if reactor_running? and @reactor_pid != Process.pid
+ # Reactor was started in a different parent, meaning we have forked.
+ # Clean up reactor state so a new reactor boots up in this child.
+ stop_event_loop
+ release_machine
+ @reactor_running = false
+ end
tail and @tails.unshift(tail)
@@ -169,6 +175,7 @@ def self.run blk=nil, tail=nil, &block
@next_tick_queue ||= []
@tails ||= []
begin
+ @reactor_pid = Process.pid
@reactor_running = true
initialize_event_machine
(b = blk || block) and add_timer(0, b)
@@ -252,7 +259,7 @@ def self.fork_reactor &block
if self.reactor_running?
self.stop_event_loop
self.release_machine
- self.instance_variable_set( '@reactor_running', false )
+ @reactor_running = false
end
self.run block
end
24 tests/test_basic.rb
View
@@ -245,4 +245,28 @@ def c.unbind
assert !timer_ran
assert_equal 1, num_close_scheduled
end
+
+ def test_fork_safe
+ return unless cpid = fork { exit! } rescue false
+
+ read, write = IO.pipe
+ EM.run do
+ cpid = fork do
+ write.puts "forked"
+ EM.run do
+ EM.next_tick do
+ write.puts "EM ran"
+ exit!
+ end
+ end
+ end
+ EM.stop
+ end
+ Process.waitall
+ assert_equal "forked\n", read.readline
+ assert_equal "EM ran\n", read.readline
+ ensure
+ read.close rescue nil
+ write.close rescue nil
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.