Skip to content

Commit

Permalink
Catch rare connection timeout in server recv()
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 8, 2024
1 parent 11b288a commit dda08bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.1.9'
__version__ = '3.1.10'

import multiprocessing as mp
try:
Expand Down
13 changes: 9 additions & 4 deletions portal/server_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,22 @@ def _recv(self, conn):
conn.recvbuf = buffers.RecvBuffer(maxsize=self.options.max_msg_size)
try:
conn.recvbuf.recv(conn.sock)
except ConnectionResetError:
self._disconnect(conn)
except OSError as e:
# For example:
# - ConnectionResetError
# - TimeoutError: [Errno 110] Connection timed out
self._disconnect(conn, e)
return
if conn.recvbuf.done():
if self.recvq.qsize() > self.options.max_recv_queue:
raise RuntimeError('Too many incoming messages enqueued')
self.recvq.put((conn.addr, conn.recvbuf.result()))
conn.recvbuf = None

def _disconnect(self, conn):
self._log(f'Closed connection to {conn.addr[0]}:{conn.addr[1]}')
def _disconnect(self, conn, e):
detail = f'{type(e).__name__}'
detail = f'{detail}: {e}' if str(e) else detail
self._log(f'Closed connection to {conn.addr[0]}:{conn.addr[1]} ({detail})')
conn = self.conns.pop(conn.addr)
if conn.sendbufs:
count = len(conn.sendbufs)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def test_tree(self, data):
client.close()
server.close()

def test_shape_mismatch(self):
@pytest.mark.parametrize('repeat', range(5))
def test_shape_mismatch(self, repeat):
port = portal.free_port()
server = portal.BatchServer(port, errors=False)
server.bind('fn', lambda x: x, batch=2)
Expand Down

0 comments on commit dda08bb

Please sign in to comment.