From 05b1ccb6585840df0835594d0884444cafb982cb Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 1 Feb 2019 14:37:13 +0000 Subject: [PATCH] ansible: stash PID files in CWD if requested for debugging. --- ansible_mitogen/process.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ansible_mitogen/process.py b/ansible_mitogen/process.py index de3560f7..8137af9c 100644 --- a/ansible_mitogen/process.py +++ b/ansible_mitogen/process.py @@ -94,6 +94,24 @@ def getenv_int(key, default=0): return default +def save_pid(name): + """ + When debugging and profiling, it is very annoying to poke through the + process list to discover the currently running Ansible and MuxProcess IDs, + especially when trying to catch an issue during early startup. So here, if + a magic environment variable set, stash them in hidden files in the CWD:: + + alias muxpid="cat .ansible-mux.pid" + alias anspid="cat .ansible-controller.pid" + + gdb -p $(muxpid) + perf top -p $(anspid) + """ + if os.environ.get('MITOGEN_SAVE_PIDS'): + with open('.ansible-%s.pid' % (name,), 'w') as fp: + fp.write(str(os.getpid())) + + class MuxProcess(object): """ Implement a subprocess forked from the Ansible top-level, as a safe place @@ -173,11 +191,13 @@ class MuxProcess(object): if _init_logging: ansible_mitogen.logging.setup() if cls.child_pid: + save_pid('controller') ansible_mitogen.affinity.policy.assign_controller() cls.child_sock.close() cls.child_sock = None mitogen.core.io_op(cls.worker_sock.recv, 1) else: + save_pid('mux') ansible_mitogen.affinity.policy.assign_muxprocess() cls.worker_sock.close() cls.worker_sock = None