Merge pull request #3273 from meeseeksmachine/auto-backport-of-pr-3237-on-1.2.x

Backport PR #3237 on branch 1.2.x ([proxy.py] Improve robustness when detecting and closing existing proxy processes)
This commit is contained in:
Min RK
2020-11-26 10:01:46 +01:00
committed by GitHub

View File

@@ -497,6 +497,19 @@ class ConfigurableHTTPProxy(Proxy):
if not psutil.pid_exists(pid): if not psutil.pid_exists(pid):
raise ProcessLookupError raise ProcessLookupError
try:
process = psutil.Process(pid)
if self.command and self.command[0]:
process_cmd = process.cmdline()
if process_cmd and not any(
self.command[0] in clause for clause in process_cmd
):
raise ProcessLookupError
except (psutil.AccessDenied, psutil.NoSuchProcess):
# If there is a process at the proxy's PID but we don't have permissions to see it,
# then it is unlikely to actually be the proxy.
raise ProcessLookupError
else: else:
os.kill(pid, 0) os.kill(pid, 0)
@@ -692,8 +705,17 @@ class ConfigurableHTTPProxy(Proxy):
parent = psutil.Process(pid) parent = psutil.Process(pid)
children = parent.children(recursive=True) children = parent.children(recursive=True)
for child in children: for child in children:
child.kill() child.terminate()
psutil.wait_procs(children, timeout=5) gone, alive = psutil.wait_procs(children, timeout=5)
for p in alive:
p.kill()
# Clear the shell, too, if it still exists.
try:
parent.terminate()
parent.wait(timeout=5)
parent.kill()
except psutil.NoSuchProcess:
pass
def _terminate(self): def _terminate(self):
"""Terminate our process""" """Terminate our process"""