From d0dad84ffaa5ef255676391c478b5fcfa67d034b Mon Sep 17 00:00:00 2001 From: Alejandro del Castillo Date: Thu, 12 Apr 2018 13:49:10 -0500 Subject: [PATCH] ConfigurableHTTPProxy.stop: kill child processes on Windows case On the Windows case, the configurable-http-proxy is spwaned using a shell. To stop the proxy, we need to terminate both the main process (shell) and its child (proxy). Signed-off-by: Alejandro del Castillo --- jupyterhub/proxy.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/jupyterhub/proxy.py b/jupyterhub/proxy.py index 2038501e..8d25f132 100644 --- a/jupyterhub/proxy.py +++ b/jupyterhub/proxy.py @@ -516,13 +516,26 @@ class ConfigurableHTTPProxy(Proxy): self._check_running_callback = pc pc.start() + def _kill_proc_tree(self, pid): + import psutil + parent = psutil.Process(pid) + children = parent.children(recursive=True) + for child in children: + child.kill() + psutil.wait_procs(children, timeout=5) + def stop(self): self.log.info("Cleaning up proxy[%i]...", self.proxy_process.pid) if self._check_running_callback is not None: self._check_running_callback.stop() if self.proxy_process.poll() is None: try: - self.proxy_process.terminate() + if os.name == 'nt': + # On Windows we spawned a shell on Popen, so we need to + # terminate all child processes as well + self._kill_proc_tree(self.proxy_process.pid) + else: + self.proxy_process.terminate() except Exception as e: self.log.error("Failed to terminate proxy process: %s", e)