Skip to content

Commit

Permalink
Don't resume fibers directly from event loop callbacks (fixes #8044). (
Browse files Browse the repository at this point in the history
…#8058)

This is required to enable compatibility with libevent 2.1.11, because
a warning is now raised if a `fork` is executed from within the event loop (libevent/libevent@497ef90).
Since most Crystal code actually runs from within the event loop (from libevent point of view)
the solution is enqueue the fibers and resume them outside the call to `event_base_loop`.
  • Loading branch information
waj authored and asterite committed Aug 8, 2019
1 parent 285d16f commit 284fb1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/crystal/event_loop.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ module Crystal::EventLoop
end

private def self.loop_fiber
@@loop_fiber ||= Fiber.new { @@eb.run_loop }
@@loop_fiber ||= Fiber.new do
loop do
@@eb.run_once
Crystal::Scheduler.reschedule
end
end
end

def self.create_resume_event(fiber)
@@eb.new_event(-1, LibEvent2::EventFlags::None, fiber) do |s, flags, data|
data.as(Fiber).resume
Crystal::Scheduler.enqueue data.as(Fiber)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/io/evented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module IO::Evented
@read_timed_out = timed_out

if reader = @readers.try &.shift?
reader.resume
Crystal::Scheduler.enqueue reader
end
end

Expand All @@ -108,7 +108,7 @@ module IO::Evented
@write_timed_out = timed_out

if writer = @writers.try &.shift?
writer.resume
Crystal::Scheduler.enqueue writer
end
end

Expand Down

0 comments on commit 284fb1e

Please sign in to comment.