From d7967948d3f7420e6988e41816f4d687ea9dceae Mon Sep 17 00:00:00 2001 From: Ondrej Balaz Date: Sat, 13 May 2023 19:37:59 +0900 Subject: [PATCH] avoid connection leaks (#80532) In case TaskExecutor needs to open a new connection it should attempt to properly dispose existing one if any. fixes: #80532 --- lib/ansible/executor/task_executor.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index a0e9c110a84..e13ccfa0c3a 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -578,6 +578,14 @@ class TaskExecutor: # pc compare, left here for old plugins, but should be irrelevant for those # using get_option, since they are cleared each iteration. self._play_context.remote_addr != self._connection._play_context.remote_addr): + # attempt to close existing connection if any + if self._connection: + try: + self._connection.close() + except AttributeError: + pass + except Exception as e: + display.debug(u"error closing connection: %s" % to_text(e)) self._connection = self._get_connection(cvars, templar, current_connection) else: # if connection is reused, its _play_context is no longer valid and needs