From 823ab8d6ace162387c126471de09c01fd4455182 Mon Sep 17 00:00:00 2001 From: Ferenc- Date: Fri, 24 Feb 2017 14:53:41 +0100 Subject: [PATCH] Handle EINTR the standard POSIX way --- thriftpy/transport/socket.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/thriftpy/transport/socket.py b/thriftpy/transport/socket.py index 51a5283..c720f45 100644 --- a/thriftpy/transport/socket.py +++ b/thriftpy/transport/socket.py @@ -104,21 +104,27 @@ def open(self): message="Could not connect to %s" % str(addr)) def read(self, sz): - try: - buff = self.sock.recv(sz) - except socket.error as e: - if (e.args[0] == errno.ECONNRESET and - (sys.platform == 'darwin' or - sys.platform.startswith('freebsd'))): - # freebsd and Mach don't follow POSIX semantic of recv - # and fail with ECONNRESET if peer performed shutdown. - # See corresponding comment and code in TSocket::read() - # in lib/cpp/src/transport/TSocket.cpp. - self.close() - # Trigger the check to raise the END_OF_FILE exception below. - buff = '' + while True: + try: + buff = self.sock.recv(sz) + except socket.error as e: + if e.errno == errno.EINTR: + continue + if (e.args[0] == errno.ECONNRESET and + (sys.platform == 'darwin' or + sys.platform.startswith('freebsd'))): + # freebsd and Mach don't follow POSIX semantic of recv + # and fail with ECONNRESET if peer performed shutdown. + # See corresponding comment and code in TSocket::read() + # in lib/cpp/src/transport/TSocket.cpp. + self.close() + # Trigger the check to raise the END_OF_FILE exception. + buff = '' + break + else: + raise else: - raise + break if len(buff) == 0: raise TTransportException(type=TTransportException.END_OF_FILE,