summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Hirschhorn <max.hirschhorn@mongodb.com>2019-03-05 13:47:00 -0500
committerMax Hirschhorn <max.hirschhorn@mongodb.com>2019-03-05 13:47:00 -0500
commit006233d0e02d6c902ba2ebd468f0314212d01bf4 (patch)
tree99c387ac8040e9cab1ca8e0a660bc84a0c546e65
parent8ee639f4e80a24a60984502bb93ddb2caddade81 (diff)
downloadmongo-006233d0e02d6c902ba2ebd468f0314212d01bf4.tar.gz
SERVER-38697 Use process creation time to detect pid reuse.
Changes start_cmd() to use psutil.Popen() rather than subprocess.Popen() in order to cache the creation time of the child process. (cherry picked from commit d43369c671a596ee816f44038fca3423a0a33126)
-rwxr-xr-xpytests/powertest.py47
1 files changed, 19 insertions, 28 deletions
diff --git a/pytests/powertest.py b/pytests/powertest.py
index 543d67c6fce..ccd0f3ac4db 100755
--- a/pytests/powertest.py
+++ b/pytests/powertest.py
@@ -67,6 +67,10 @@ if os.name == "posix" and sys.version_info[0] == 2:
else:
import subprocess
+# We replace the subprocess module imported by the psutil package so we can safely use
+# psutil.Popen() in addition to subprocess.Popen().
+psutil.subprocess = subprocess
+
# Get relative imports to work when the package is not installed on the PYTHONPATH.
if __name__ == "__main__" and __package__ is None:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@@ -252,42 +256,27 @@ def dump_stacks_and_exit(signum, frame):
sys.exit(1)
-def child_processes(parent_pid):
- """Returns a list of all child processes for a pid."""
- # The child processes cannot be obtained from the parent on Windows from psutil. See
- # https://stackoverflow.com/questions/30220732/python-psutil-not-showing-all-child-processes
- child_procs = []
- while psutil.pid_exists(parent_pid):
- try:
- child_procs = [p for p in psutil.process_iter(attrs=["pid"]) if parent_pid == p.ppid()]
- break
- except psutil.NoSuchProcess:
- pass
- for proc in child_procs:
- proc_children = child_processes(proc.pid)
- if proc_children:
- child_procs += proc_children
- return list(set(child_procs))
-
-
-def kill_process(pid, kill_children=True):
+def kill_process(parent, kill_children=True):
"""Kill a process, and optionally it's children, by it's pid. Returns 0 if successful."""
try:
- parent = psutil.Process(pid)
+ # parent.children() implicitly calls parent.is_running(), which raises a
+ # psutil.NoSuchProcess exception if the creation time for the process with pid=parent.pid is
+ # different than parent.create_time(). We can reliably detect pid reuse this way because
+ # 'parent' is the same psutil.Process instance returned by start_cmd() and therefore has an
+ # accurate notion of the creation time.
+ procs = parent.children(recursive=True) if kill_children else []
except psutil.NoSuchProcess:
- LOGGER.warn("Could not kill process %d, as it no longer exists", pid)
+ LOGGER.warn("Could not kill process %d, as it no longer exists", parent.pid)
return 0
- procs = [parent]
- if kill_children:
- procs += child_processes(pid)
+ procs.append(parent)
for proc in procs:
try:
LOGGER.debug("Killing process '%s' pid %d", proc.name(), proc.pid)
proc.kill()
except psutil.NoSuchProcess:
- LOGGER.warn("Could not kill process %d, as it no longer exists", pid)
+ LOGGER.warn("Could not kill process %d, as it no longer exists", proc.pid)
_, alive = psutil.wait_procs(procs, timeout=30, callback=None)
if alive:
@@ -300,7 +289,7 @@ def kill_processes(procs, kill_children=True):
"""Kill a list of processes and optionally it's children."""
for proc in procs:
LOGGER.debug("Starting kill of parent process %d", proc.pid)
- kill_process(proc.pid, kill_children=kill_children)
+ kill_process(proc, kill_children=kill_children)
ret = proc.wait()
LOGGER.debug("Finished kill of parent process %d has return code of %d", proc.pid, ret)
@@ -377,8 +366,10 @@ def start_cmd(cmd, use_file=False):
else:
LOGGER.debug("Executing '%s'", cmd)
- proc = subprocess.Popen(cmd, close_fds=True)
- LOGGER.debug("Spawned process %s pid %d", psutil.Process(proc.pid).name(), proc.pid)
+ # We use psutil.Popen() rather than subprocess.Popen() in order to cache the creation time of
+ # the process. This enables us to reliably detect pid reuse in kill_process().
+ proc = psutil.Popen(cmd, close_fds=True)
+ LOGGER.debug("Spawned process %s pid %d", proc.name(), proc.pid)
return proc