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: 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))