From 3fadf4a1cbfe64ad1827248c0e9b73a2b38093cf Mon Sep 17 00:00:00 2001 From: dw Date: Tue, 4 Jun 2019 17:45:20 +0100 Subject: [PATCH] Fix file descriptor leak in lineinfile module. (#57328) --- changelogs/fragments/57327-fix-file-descriptor-leak.yaml | 2 ++ lib/ansible/modules/files/lineinfile.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/57327-fix-file-descriptor-leak.yaml diff --git a/changelogs/fragments/57327-fix-file-descriptor-leak.yaml b/changelogs/fragments/57327-fix-file-descriptor-leak.yaml new file mode 100644 index 00000000000..cee6d4f066e --- /dev/null +++ b/changelogs/fragments/57327-fix-file-descriptor-leak.yaml @@ -0,0 +1,2 @@ +bugfixes: +- lineinfile - fix a race / file descriptor leak when writing the file (https://github.com/ansible/ansible/issues/57327) diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index 698bd4872f1..86bc44e4f2e 100644 --- a/lib/ansible/modules/files/lineinfile.py +++ b/lib/ansible/modules/files/lineinfile.py @@ -215,7 +215,7 @@ from ansible.module_utils._text import to_bytes, to_native def write_changes(module, b_lines, dest): tmpfd, tmpfile = tempfile.mkstemp() - with open(tmpfile, 'wb') as f: + with os.fdopen(tmpfd, 'wb') as f: f.writelines(b_lines) validate = module.params.get('validate', None)