forked from M-Labs/artiq
protocol/logging: workaround for asyncio's inability to detect connection closes on writes
This commit is contained in:
parent
f332c1d3cc
commit
6c856025cc
|
@ -117,11 +117,19 @@ class LogForwarder(logging.Handler, TaskObject):
|
||||||
try:
|
try:
|
||||||
reader, writer = await asyncio.open_connection(self.host,
|
reader, writer = await asyncio.open_connection(self.host,
|
||||||
self.port)
|
self.port)
|
||||||
|
detect_close = asyncio.ensure_future(reader.read(1))
|
||||||
writer.write(_init_string)
|
writer.write(_init_string)
|
||||||
while True:
|
while True:
|
||||||
message = await self._queue.get() + "\n"
|
message = await self._queue.get() + "\n"
|
||||||
writer.write(message.encode())
|
writer.write(message.encode())
|
||||||
await writer.drain()
|
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:
|
except asyncio.CancelledError:
|
||||||
return
|
return
|
||||||
except:
|
except:
|
||||||
|
|
Loading…
Reference in New Issue