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)