From 8e1cc3fe20fa813e2f222b8aec212ec9e4e2c686 Mon Sep 17 00:00:00 2001 From: jdholtz Date: Wed, 12 Jul 2023 23:21:37 -0500 Subject: [PATCH] Wait for Process IDs to terminate after killing them This prevents zombie (defunct) processes on *nix systems. The waitpid() function throws a ChildProcessError on Windows, so that is handled accordingly. --- undetected_chromedriver/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/undetected_chromedriver/__init__.py b/undetected_chromedriver/__init__.py index 2139c58..3047d04 100644 --- a/undetected_chromedriver/__init__.py +++ b/undetected_chromedriver/__init__.py @@ -751,8 +751,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): def quit(self): try: self.service.process.kill() + os.waitpid(self.service.process.pid, 0) logger.debug("webdriver process ended") - except (AttributeError, RuntimeError, OSError): + except (AttributeError, ChildProcessError, RuntimeError, OSError): pass try: self.reactor.event.set() @@ -761,6 +762,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): pass try: os.kill(self.browser_pid, 15) + os.waitpid(self.browser_pid, 0) logger.debug("gracefully closed browser") except Exception as e: # noqa pass @@ -840,6 +842,12 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): ): self.service.process.kill() + try: + # Prevent zombie processes + os.waitpid(self.service.process.pid, 0) + except ChildProcessError: + pass + def find_chrome_executable(): """