summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2013-10-24 19:02:13 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2013-10-24 19:02:13 +0100
commit09b6b9450915d4605c0349730ea3504d86398c93 (patch)
tree5baa16a95596736851cb46b47de84dc11b1e4ee2 /lib/pure
parentf5023ac54407fd253c593cd0c68733f18412eb02 (diff)
downloadNim-09b6b9450915d4605c0349730ea3504d86398c93.tar.gz
Fixed asyncio crash when sending buffered data after connection was terminated.
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/asyncio.nim23
1 files changed, 14 insertions, 9 deletions
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))