Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Handle EINTR the standard POSIX way #281

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions thriftpy/transport/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down