From 1eb1fbdfc9741664d6d053ec70fc8f373889594b Mon Sep 17 00:00:00 2001 From: Sandro Pischinger Date: Wed, 29 Jan 2025 16:45:36 +0100 Subject: [PATCH 1/2] service: only shutdown if process not terminated There are scenarios where a stop() is called on a Service object multiple times. This also happens when explicitly quitting a Webdriver using driver.quit() and afterwards having the garbage collector destroy the service object, calling stop() another time even though the service process has already terminated. The check inside the stop() call only ensured that the process variable is not None, but ignored the fact that the process might already have terminated. Therefor an additional check is introduced to only send the remote shutdown command if the service process has not ended. Fixes #15182 Signed-off-by: Sandro Pischinger --- py/selenium/webdriver/common/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/service.py b/py/selenium/webdriver/common/service.py index 9c592ba75a525..306384546dcd3 100644 --- a/py/selenium/webdriver/common/service.py +++ b/py/selenium/webdriver/common/service.py @@ -152,7 +152,7 @@ def stop(self) -> None: elif isinstance(self.log_output, int): os.close(self.log_output) - if self.process is not None: + if self.process is not None and self.process.poll() is None: try: self.send_remote_shutdown_command() except TypeError: From eb1934955334b9c95054c666a5356f9615cbbb0f Mon Sep 17 00:00:00 2001 From: Sandro Pischinger Date: Sat, 22 Mar 2025 15:18:08 +0100 Subject: [PATCH 2/2] service: move process termination to finally block If a type error or similar occurs during send_remote_shutdown_command, one should always terminate the service process, even if an exception is thrown. Thus moving self._terminate_process() into a finally block. Signed-off-by: Sandro Pischinger --- py/selenium/webdriver/common/service.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/service.py b/py/selenium/webdriver/common/service.py index 306384546dcd3..d74b55ef3be18 100644 --- a/py/selenium/webdriver/common/service.py +++ b/py/selenium/webdriver/common/service.py @@ -157,7 +157,8 @@ def stop(self) -> None: self.send_remote_shutdown_command() except TypeError: pass - self._terminate_process() + finally: + self._terminate_process() def _terminate_process(self) -> None: """Terminate the child process.