From 6c856025cc1866e6f18db2849e79336ff08efdce Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 16 Oct 2015 21:28:39 +0800 Subject: [PATCH] protocol/logging: workaround for asyncio's inability to detect connection closes on writes --- artiq/protocols/logging.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/artiq/protocols/logging.py b/artiq/protocols/logging.py index 61b9c07fb..6333942da 100644 --- a/artiq/protocols/logging.py +++ b/artiq/protocols/logging.py @@ -117,11 +117,19 @@ class LogForwarder(logging.Handler, TaskObject): try: reader, writer = await asyncio.open_connection(self.host, self.port) + detect_close = asyncio.ensure_future(reader.read(1)) writer.write(_init_string) while True: message = await self._queue.get() + "\n" writer.write(message.encode()) await writer.drain() + # HACK: detect connection termination through the completion + # of a read operation. For some reason, write/drain operations + # on a closed socket do not raise exceptions, but print + # "asyncio:socket.send() raised exception." + if detect_close.done(): + await asyncio.sleep(self.reconnect_timer) + break except asyncio.CancelledError: return except: