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.
This commit is contained in:
jdholtz 2023-07-12 23:21:37 -05:00
parent 1c704a71cf
commit 8e1cc3fe20
No known key found for this signature in database
GPG Key ID: A3A87CFD1E4A1B65
1 changed files with 9 additions and 1 deletions

View File

@ -751,8 +751,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
def quit(self): def quit(self):
try: try:
self.service.process.kill() self.service.process.kill()
os.waitpid(self.service.process.pid, 0)
logger.debug("webdriver process ended") logger.debug("webdriver process ended")
except (AttributeError, RuntimeError, OSError): except (AttributeError, ChildProcessError, RuntimeError, OSError):
pass pass
try: try:
self.reactor.event.set() self.reactor.event.set()
@ -761,6 +762,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
pass pass
try: try:
os.kill(self.browser_pid, 15) os.kill(self.browser_pid, 15)
os.waitpid(self.browser_pid, 0)
logger.debug("gracefully closed browser") logger.debug("gracefully closed browser")
except Exception as e: # noqa except Exception as e: # noqa
pass pass
@ -840,6 +842,12 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
): ):
self.service.process.kill() self.service.process.kill()
try:
# Prevent zombie processes
os.waitpid(self.service.process.pid, 0)
except ChildProcessError:
pass
def find_chrome_executable(): def find_chrome_executable():
""" """