From 09b6b9450915d4605c0349730ea3504d86398c93 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 24 Oct 2013 19:02:13 +0100 Subject: Fixed asyncio crash when sending buffered data after connection was terminated. --- lib/pure/asyncio.nim | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/asyncio.nim b/lib/pure/asyncio.nim index 48a22bbe8..a0cf657bf 100644 --- a/lib/pure/asyncio.nim +++ b/lib/pure/asyncio.nim @@ -213,6 +213,7 @@ proc asyncSockHandleRead(h: PObject) = else: PAsyncSocket(h).handleAccept(PAsyncSocket(h)) +proc close*(sock: PAsyncSocket) proc asyncSockHandleWrite(h: PObject) = when defined(ssl): if PAsyncSocket(h).socket.isSSL and not @@ -230,15 +231,19 @@ proc asyncSockHandleWrite(h: PObject) = else: if PAsyncSocket(h).sendBuffer != "": let sock = PAsyncSocket(h) - let bytesSent = sock.socket.sendAsync(sock.sendBuffer) - assert bytesSent > 0 - if bytesSent != sock.sendBuffer.len: - sock.sendBuffer = sock.sendBuffer[bytesSent .. -1] - elif bytesSent == sock.sendBuffer.len: - sock.sendBuffer = "" - - if PAsyncSocket(h).handleWrite != nil: - PAsyncSocket(h).handleWrite(PAsyncSocket(h)) + try: + let bytesSent = sock.socket.sendAsync(sock.sendBuffer) + assert bytesSent > 0 + if bytesSent != sock.sendBuffer.len: + sock.sendBuffer = sock.sendBuffer[bytesSent .. -1] + elif bytesSent == sock.sendBuffer.len: + sock.sendBuffer = "" + + if PAsyncSocket(h).handleWrite != nil: + PAsyncSocket(h).handleWrite(PAsyncSocket(h)) + except EOS, ESSL: + # Most likely the socket closed before the full buffer could be sent to it. + sock.close() # TODO: Provide a handleError for users? else: if PAsyncSocket(h).handleWrite != nil: PAsyncSocket(h).handleWrite(PAsyncSocket(h)) -- cgit 1.4.1-2-gfad0