From 3bb2056a7fce3fad51e00e5cfb5b6607020e22ad Mon Sep 17 00:00:00 2001 From: mdumandag Date: Wed, 27 Jan 2021 16:21:20 +0300 Subject: [PATCH 1/2] Cleanup resources when the connection attempt fails immediately When the socket.connect fails immediately, we were not removing the connection from the dispatchers map and closing the in memory write buffer. --- hazelcast/reactor.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/hazelcast/reactor.py b/hazelcast/reactor.py index a402a9776d..42c47cc6e7 100644 --- a/hazelcast/reactor.py +++ b/hazelcast/reactor.py @@ -392,7 +392,14 @@ def __init__( if config.ssl_enabled: self._wrap_as_ssl_socket(config) - self.connect((address.host, address.port)) + try: + self.connect((address.host, address.port)) + except socket.error as e: + # If the connection attempt failed + # immediately, remove the connection from + # the dispatchers map and clean resources. + self._inner_close() + raise e timeout = config.connection_timeout if timeout > 0: From 368f30b2f5b22b02f0bd9f182244e386ecf47f54 Mon Sep 17 00:00:00 2001 From: mdumandag Date: Wed, 3 Feb 2021 13:00:25 +0300 Subject: [PATCH 2/2] add a test for the failure scenario --- tests/reactor_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/reactor_test.py b/tests/reactor_test.py index 43e2bedae1..d6d6a705a5 100644 --- a/tests/reactor_test.py +++ b/tests/reactor_test.py @@ -349,3 +349,15 @@ def test_constructor_with_unreachable_addresses(self): self.assertLess(time.time() - start, config.connection_timeout) finally: conn.close(None, None) + + def test_resources_cleaned_up_after_immediate_failure(self): + addr = Address("invalid-address", 5701) + config = _Config() + mock_reactor = MagicMock(map={}) + try: + AsyncoreConnection(mock_reactor, MagicMock(), None, addr, config, None) + self.fail("Connection attempt to an invalid address should fail immediately") + except socket.error: + # Constructor of the connection should remove itself from the + # dispatchers map of the reactor. + self.assertEqual(0, len(mock_reactor.map))