From a7eee1536fc9302f583c4d89c6169f63e9f08ed1 Mon Sep 17 00:00:00 2001 From: sivel / Matt Martz Date: Tue, 23 Sep 2025 14:59:28 -0500 Subject: [PATCH] [stable-2.19] Prevent IO capture hang/loss in `basic.run_command` (#85869) (#85891) * Prevent run_command output truncation or hang In cases when the selector used to monitor stdout/stderr activates without data ready to read (a rare but normal condition), a read from a non-blocking FD can return `None`, which was being conflated with an empty read (EOF) condition. This caused the selector to be unregistered prematurely, sometimes resulting in truncated output or hangs. `None` read results are now excluded from EOF conditions. * add changelog --------- (cherry picked from commit 79ddee1) Co-authored-by: Chris Meyers Co-authored-by: Matt Davis --- changelogs/fragments/run_command_output_selector.yml | 2 ++ lib/ansible/module_utils/basic.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/run_command_output_selector.yml diff --git a/changelogs/fragments/run_command_output_selector.yml b/changelogs/fragments/run_command_output_selector.yml new file mode 100644 index 00000000000..22f2c968448 --- /dev/null +++ b/changelogs/fragments/run_command_output_selector.yml @@ -0,0 +1,2 @@ +bugfixes: + - run_command - Fixed premature selector unregistration on empty read from stdout/stderr that caused truncated output or hangs in rare situations. diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 5b5a05e8efc..116bfdec570 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -2090,7 +2090,7 @@ class AnsibleModule(object): stdout_changed = False for key, event in events: b_chunk = key.fileobj.read(32768) - if not b_chunk: + if not b_chunk and b_chunk is not None: selector.unregister(key.fileobj) elif key.fileobj == cmd.stdout: stdout += b_chunk