Commit 1b9e9926 authored by Antoine Millet's avatar Antoine Millet
Browse files

Enhanced socket read error handling

parent f4b653ff
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ class are located in it docstring.
from __future__ import absolute_import

import ssl
import errno
import struct
import socket
import logging
@@ -52,6 +53,9 @@ class RpcConnection(object):
        automatically registered on label 0.
    '''

    NONBLOCKING_ERRORS = (errno.EAGAIN, errno.EWOULDBLOCK)
    NONBLOCKING_SSL_ERRORS = (ssl.SSL_ERROR_WANT_READ)

    MESSAGE_HEADER = '!HL'
    MESSAGE_HEADER_FALLBACK = '!L'
    MAX_LABEL = 2 ** 16
@@ -189,8 +193,18 @@ class RpcConnection(object):
        handler.
        '''
        # Try to received remaining data from the socket:
        buf = self._sock.recv(self._remains)
        if buf == '':
        try:
            buf = self._sock.recv(self._remains)
        except socket.error as err:
            if (isinstance(err, socket.error) and err.errno
                in RpcConnection.NONBLOCKING_ERRORS):
                return
            elif (isinstance(err, ssl.SSLError) and err.errno
                  in RpcConnection.NONBLOCKING_SSL_ERRORS):
                return
            else:
                raise
            self.shutdown()
        self._remains -= len(buf)