As a simple example, consider the network equivalent of /dev/zero, below. (Or more realistically, just a web server sending a large file.)
If a client disconnects early, you get a barrage of log messages:
WARNING:asyncio:socket.send() raised exception.
But I'm not finding any way to catch said exception. The hypothetical server continues reading gigabytes from disk and sending them to a dead socket, with no effort on the client's part, and you've got yourself a DoS attack.
The only thing I've found from the docs is to yield from a read, with an empty string indicating closure. But that's no good here because a normal client isn't going to send anything, blocking the write loop.
What's the right way to detect failed writes, or be notified that the TCP connection has been closed, with the streams API or otherwise?
Code:
from asyncio import *
import logging
@coroutine
def client_handler(reader, writer):
while True:
writer.write(bytes(1))
yield from writer.drain()
logging.basicConfig(level=logging.INFO)
loop = get_event_loop()
coro = start_server(client_handler, '', 12345)
server = loop.run_until_complete(coro)
loop.run_forever()
I did some digging into the
asyncio
source to expand on dano's answer on why the exceptions aren't being raised without explicitly passing control to the event loop. Here's what I've found.Calling
yield from wirter.drain()
gives the control over to theStreamWriter.drain
coroutine. This coroutine checks for and raises any exceptions that that theStreamReaderProtocol
set on theStreamReader
. But since we passed control over todrain
, the protocol hasn't had the chance to set the exception yet.drain
then gives control over to theFlowControlMixin._drain_helper
coroutine. This coroutine the returns immediately because some more flags haven't been set yet, and the control ends up back with the coroutine that calledyield from wirter.drain()
.And so we have gone full circle without giving control to the event loop to allow it handle other coroutines and bubble up the exceptions to
writer.drain()
.yield
ing before adrain()
gives the transport/protocol a chance to set the appropriate flags and exceptions.Here's a mock up of what's going on, with all the nested calls collapsed:
This should probably fixed upstream in the Streams API by the
asyncio
developers.This is a little bit strange, but you can actually allow an exception to reach the
client_handler
coroutine by forcing it to yield control to the event loop for one iteration:If I do that, I get this output when I kill the client connection:
I'm really not quite sure why you need to explicitly let the event loop get control for the exception to get through - don't have time at the moment to dig into it. I assume some bit needs to get flipped to indicate the connection dropped, and calling
yield from writer.drain()
(which can short-circuit going through the event loop) in a loop is preventing that from happening, but I'm really not sure. If I get a chance to investigate, I'll update the answer with that info.The stream based API doesn't have a callback you can specify for when the connection is closed. But the Protocol API does, so use it instead: https://docs.python.org/3/library/asyncio-protocol.html#connection-callbacks