From 549b25fd501b0df4a4a8b5edccde6101edb0cd63 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Jul 2010 10:09:53 +0000 Subject: event_machine: avoid race in unchunked fast pipe responses Using EM.enable_proxy with EM.attach seems to cause EM::Connection#receive_data callbacks to be fired before the proxy has a chance to act, leading the first few chunks of data being lost in the default receive_data handler. Instead just rely on EM.watch like the chunked pipe. --- lib/rainbows/event_machine.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/rainbows/event_machine.rb') diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb index 77fc962..4d5156e 100644 --- a/lib/rainbows/event_machine.rb +++ b/lib/rainbows/event_machine.rb @@ -106,11 +106,8 @@ module Rainbows else do_chunk = false end - if do_chunk - EM.watch(io, ResponseChunkPipe, self).notify_readable = true - else - EM.enable_proxy(EM.attach(io, ResponsePipe, self), self, 16384) - end + mod = do_chunk ? ResponseChunkPipe : ResponsePipe + EM.watch(io, mod, self).notify_readable = true end def em_write_response(response, alive = false) @@ -165,6 +162,19 @@ module Rainbows @client = client end + def notify_readable + begin + @client.write(@io.read_nonblock(16384)) + rescue Errno::EINTR + retry + rescue Errno::EAGAIN + return + rescue EOFError + detach + return + end while true + end + def unbind @io.close @client.quit -- cgit v1.2.3-24-ge0c7