From fb09fd2a2301db485f5e15f77b59c31e7bc1645a Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Thu, 29 Oct 2020 21:40:31 -0400 Subject: [PATCH] wait_for - ignore psutil related errors (#72401) When enumerating connections with psutil, catch and ignore errors to avoid returning a stack trace. Co-authored-by: Matt Martz --- .../fragments/72322-wait-for-handle-errors.yml | 2 ++ lib/ansible/modules/wait_for.py | 12 ++++++++---- test/integration/targets/wait_for/files/zombie.py | 13 +++++++++++++ test/integration/targets/wait_for/tasks/main.yml | 10 ++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/72322-wait-for-handle-errors.yml create mode 100644 test/integration/targets/wait_for/files/zombie.py diff --git a/changelogs/fragments/72322-wait-for-handle-errors.yml b/changelogs/fragments/72322-wait-for-handle-errors.yml new file mode 100644 index 00000000000..d32940fa234 --- /dev/null +++ b/changelogs/fragments/72322-wait-for-handle-errors.yml @@ -0,0 +1,2 @@ +bugfixes: + - wait_for - catch and ignore errors when getting active connections with psutil (https://github.com/ansible/ansible/issues/72322) diff --git a/lib/ansible/modules/wait_for.py b/lib/ansible/modules/wait_for.py index 26287f02fd8..f87e46f0c57 100644 --- a/lib/ansible/modules/wait_for.py +++ b/lib/ansible/modules/wait_for.py @@ -289,10 +289,14 @@ class TCPConnectionInfo(object): def get_active_connections_count(self): active_connections = 0 for p in psutil.process_iter(): - if hasattr(p, 'get_connections'): - connections = p.get_connections(kind='inet') - else: - connections = p.connections(kind='inet') + try: + if hasattr(p, 'get_connections'): + connections = p.get_connections(kind='inet') + else: + connections = p.connections(kind='inet') + except psutil.Error: + # Process is Zombie or other error state + continue for conn in connections: if conn.status not in self.module.params['active_connection_states']: continue diff --git a/test/integration/targets/wait_for/files/zombie.py b/test/integration/targets/wait_for/files/zombie.py new file mode 100644 index 00000000000..913074eb909 --- /dev/null +++ b/test/integration/targets/wait_for/files/zombie.py @@ -0,0 +1,13 @@ +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import os +import sys +import time + +child_pid = os.fork() + +if child_pid > 0: + time.sleep(60) +else: + sys.exit() diff --git a/test/integration/targets/wait_for/tasks/main.yml b/test/integration/targets/wait_for/tasks/main.yml index 5522030551b..67e077868ed 100644 --- a/test/integration/targets/wait_for/tasks/main.yml +++ b/test/integration/targets/wait_for/tasks/main.yml @@ -153,6 +153,16 @@ name: psutil when: ansible_system != 'Linux' +- name: Copy zombie.py + copy: + src: zombie.py + dest: "{{ output_dir }}" + +- name: Create zombie process + shell: "{{ ansible_python.executable }} {{ output_dir }}/zombie" + async: 90 + poll: 0 + - name: test wait for port drained wait_for: port: "{{ http_port }}"