Close subprocess stdout and stderr in the ssh connection plugin

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  by Julien Palard
Fixes 
This commit is contained in:
Julien Palard 2019-11-14 17:35:30 +01:00 committed by Sviatoslav Sydorenko
parent 67fd17bee4
commit 81b334bcaf

View file

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