# HG changeset patch
# User Tony Garnock-Jones <tonygarnockjones@gmail.com>
# Date 1321390387 18000
#      Tue Nov 15 15:53:07 2011 -0500
# Node ID f62523737dcdfb1e13c93983d2635059924b4472
# Parent  3336aee3ea21435d761d294481aefa9ebd68b532
Avoid hanging forever when the socket goes away and the only thing we're doing is writing.

If the connection closed during a flush_outbound with pending data, we
were in some cases (asyncore-based) looping forever. Checking each
time we enter the loop gets us a sensible exception in these cases.

diff --git a/pika/asyncore_adapter.py b/pika/asyncore_adapter.py
--- a/pika/asyncore_adapter.py
+++ b/pika/asyncore_adapter.py
@@ -54,6 +54,7 @@
 from heapq import heappush, heappop
 from errno import EAGAIN
 import pika.connection
+from pika.exceptions import *
 
 class RabbitDispatcher(asyncore.dispatcher):
     def __init__(self, connection):
@@ -108,6 +109,9 @@
 
     def flush_outbound(self):
         while self.outbound_buffer:
+            if self.connection_close:
+                # The connection was closed while we weren't looking!
+                raise ConnectionClosed(self.connection_close)
             self.drain_events()
 
     def drain_events(self):