Skip to content

Commit

Permalink
Add a test and @tmm1s patch for making post-fork EM.run calls work as…
Browse files Browse the repository at this point in the history
… expected.

Closes eventmachine#213
  • Loading branch information
raggi committed Sep 29, 2011
1 parent 8cd81fb commit 427a76d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/eventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/test_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 427a76d

Please sign in to comment.