Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THRIFT-5777: python fix mismatched timeout exceptions. #2961

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/py/src/transport/TSocket.py
Expand Up @@ -151,6 +151,8 @@ def open(self):
def read(self, sz):
try:
buff = self.handle.recv(sz)
except socket.timeout as e:
raise TTransportException(type=TTransportException.TIMED_OUT, message="read timeout", inner=e)
except socket.error as e:
if (e.args[0] == errno.ECONNRESET and
(sys.platform == 'darwin' or sys.platform.startswith('freebsd'))):
Expand All @@ -161,8 +163,6 @@ def read(self, sz):
self.close()
# Trigger the check to raise the END_OF_FILE exception below.
buff = ''
elif e.args[0] == errno.ETIMEDOUT:
raise TTransportException(type=TTransportException.TIMED_OUT, message="read timeout", inner=e)
else:
raise TTransportException(message="unexpected exception", inner=e)
if len(buff) == 0:
Expand Down
18 changes: 18 additions & 0 deletions lib/py/test/test_socket.py
Expand Up @@ -11,6 +11,24 @@


class TSocketTest(unittest.TestCase):
def test_socket_readtimeout_exception(self):
acc = ServerAcceptor(TServerSocket(port=0))
acc.start()

sock = TSocket(host="localhost", port=acc.port)
sock.open()
sock.setTimeout(1)
sock.write(b"sleep")

with self.assertRaises(TTransportException) as ctx:
sock.read(5)
exc = ctx.exception
self.assertEqual(exc.message, "read timeout")

acc.client.close() # this also blocks until the other thread is done
acc.close()
sock.close()

def test_isOpen_checks_for_readability(self):
# https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts
# https://docs.python.org/3/library/socket.html#socket.socket.settimeout
Expand Down
5 changes: 4 additions & 1 deletion lib/py/test/test_sslsocket.py
Expand Up @@ -19,6 +19,7 @@

import inspect
import logging
import time
import os
import platform
import ssl
Expand Down Expand Up @@ -76,7 +77,9 @@ def run(self):
try:
self._client = self._server.accept()
if self._client:
self._client.read(5) # hello
data = self._client.read(5) # hello/sleep
if data == b"sleep":
time.sleep(2)
self._client.write(b"there")
except Exception:
logging.exception('error on server side (%s):' % self.name)
Expand Down