Skip to content

Commit 07daef7

Browse files
ccorbachoauvipy
authored andcommitted
Set an explicit timeout on SSL handshake to prevent hangs
If we do not set a timeout on the SSL handshake, this can cause an infinite hang if something happens during this point to the remote end - this has been seen with AWS MQ RabbitMQ during cluster maintenance triggering a reboot, and causing hangs of any connection that is in the handshake phase.
1 parent df89ff4 commit 07daef7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

amqp/transport.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ def __init__(self, host, connect_timeout=None, ssl=None, **kwargs):
401401
def _setup_transport(self):
402402
"""Wrap the socket in an SSL object."""
403403
self.sock = self._wrap_socket(self.sock, **self.sslopts)
404+
# Explicitly set a timeout here to stop any hangs on handshake.
405+
self.sock.settimeout(self.connect_timeout)
404406
self.sock.do_handshake()
405407
self._quick_recv = self.sock.read
406408

t/unit/test_transport.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,14 @@ def test_read_SSLError(self):
864864
with pytest.raises(socket.timeout):
865865
self.t._read(64)
866866

867+
def test_handshake_timeout(self):
868+
self.t.sock = Mock()
869+
self.t._wrap_socket = Mock()
870+
self.t._wrap_socket.return_value = self.t.sock
871+
self.t.sock.do_handshake.side_effect = socket.timeout()
872+
with pytest.raises(socket.timeout):
873+
self.t._setup_transport()
874+
867875

868876
class test_TCPTransport:
869877
class Transport(transport.TCPTransport):

0 commit comments

Comments
 (0)