Fix a possible receive timeout

This patch fixes a possible receive timeout caused by a slow response from the
driver agent. For example if the database is very slow.

Change-Id: I9079030a5fef9dc44da242adab3c568666777451
This commit is contained in:
Michael Johnson 2023-08-23 00:13:39 +00:00
parent bad19004b0
commit b4c210ac58
3 changed files with 18 additions and 5 deletions

View File

@ -58,11 +58,19 @@ class DriverLibrary():
def _recv(self, sock):
size_str = b''
char = sock.recv(1)
begin = time.time()
while char != b'\n':
size_str += char
char = sock.recv(1)
while True:
try:
char = sock.recv(1)
except socket.timeout:
# We could have an overloaded DB and the query may take too
# long, so as long as DRIVER_AGENT_TIMEOUT hasn't expired,
# let's keep trying while not blocking everything.
pass
else:
if char == b'\n':
break
size_str += char
if time.time() - begin > DRIVER_AGENT_TIMEOUT:
raise driver_exceptions.DriverAgentTimeout(
fault_string=('The driver agent did not respond in {} '

View File

@ -45,7 +45,7 @@ class TestDriverLib(base.TestCase):
@mock.patch('builtins.memoryview')
def test_recv(self, mock_memoryview):
mock_socket = mock.MagicMock()
mock_socket.recv.side_effect = [b'1', b'\n', b'2', b'\n', b'3', b'\n']
mock_socket.recv.side_effect = [b'1', b'\n', b'2', b'3', b'\n']
mock_socket.recv_into.return_value = 1
mv_mock = mock.MagicMock()
mock_memoryview.return_value = mv_mock

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Fixed a possible receive timeout if the driver agent didn't respond inside
five seconds. For example if you have a very slow database.