From 6ee5297c02b6b99414a508af673a4e6c82c981db Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 3 Dec 2019 22:17:44 +0100 Subject: [PATCH] Close subprocess stdout and stderr in the ssh connection plugin (#64874) Eliminate `ResourceWarning: unclosed file` by closing all file handles of each subprocess used by the SSH connection plugin to execute commands. This change prevents Ansible from accumulating "forgotten" open file handles. PR #64785 by Julien Palard Fixes #64768 --- lib/ansible/plugins/connection/ssh.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/connection/ssh.py b/lib/ansible/plugins/connection/ssh.py index b978a5f6225..94c4eefc1ea 100644 --- a/lib/ansible/plugins/connection/ssh.py +++ b/lib/ansible/plugins/connection/ssh.py @@ -1014,9 +1014,11 @@ class Connection(ConnectionBase): # Otherwise there may still be outstanding data to read. finally: selector.close() - # close stdin after process is terminated and stdout/stderr are read - # completely (see also issue #848) + # close stdin, stdout, and stderr after process is terminated and + # stdout/stderr are read completely (see also issues #848, #64768). stdin.close() + p.stdout.close() + p.stderr.close() if C.HOST_KEY_CHECKING: if cmd[0] == b"sshpass" and p.returncode == 6: