-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
What happened?
When I use driver.quit() in the code example below, the python3 process doesn't terminate immediately after the last line of the script. Instead, two minutes need to go by before we get termination.
I assume the garbage collector calls service.stop() again at the end of the script, even though the service process is already terminated explicitly by me calling the driver.quit(). This results in the method call self.send_remote_shutdown_command()` even though the service process has already terminated.
I suspect this somehow leaves some file descriptor or similar open, when trying to call the shutdown endpoint from the service url.
| request.urlopen(f"{self.service_url}/shutdown") |
When you add a timeout (e.g. request.urlopen(f"{self.service_url}/shutdown", timeout=1)), then the python3 script is able to terminate immediately. To my understanding, we shouldn't even try to send the remote shutdown command, since the service process is already terminated.
| if self.process is not None: |
inside
service.py#stop(self), a check is done to see if the process is not None. But even if the process is not None, it might still be already terminated.
The bug doesn't appear when doing an additional check in the webdriver/common/service.py to only send to shutdown command if the process has actually not terminated yet by checking if there's no return code yet:
if self.process is not None and self.process.poll() is None:
try:
self.send_remote_shutdown_command()
[...]
How can we reproduce the issue?
requirements: `selenium==4.28.1`
and the following script (`main.py`)
import logging
from selenium import webdriver
logging.basicConfig(level=logging.DEBUG)
driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.quit()
print('finish')
then run the script (and optionally time it):
`time python main.py`Relevant log output
(.venv) [sandro@x13 ~/Downloads/selenium-bug-reproduce]$ time python3 main.py 0
DEBUG:selenium.webdriver.common.selenium_manager:Selenium Manager binary found at: /home/sandro/Downloads/selenium-bug-reproduce/.venv/lib/python3.13/site-packages/selenium/webdriver/common/linux/selenium-manager
DEBUG:selenium.webdriver.common.selenium_manager:Executing process: /home/sandro/Downloads/selenium-bug-reproduce/.venv/lib/python3.13/site-packages/selenium/webdriver/common/linux/selenium-manager --browser firefox --debug --language-binding python --output json
DEBUG:selenium.webdriver.common.selenium_manager:Found geckodriver 0.35.0 in PATH: /sbin/geckodriver
DEBUG:selenium.webdriver.common.selenium_manager:firefox detected at /usr/bin/firefox
DEBUG:selenium.webdriver.common.selenium_manager:Running command: /usr/bin/firefox -v
DEBUG:selenium.webdriver.common.selenium_manager:Output: "Mozilla Firefox 134.0"
DEBUG:selenium.webdriver.common.selenium_manager:Detected browser: firefox 134.0
DEBUG:selenium.webdriver.common.selenium_manager:Required driver: geckodriver 0.35.0
DEBUG:selenium.webdriver.common.selenium_manager:Driver path: /sbin/geckodriver
DEBUG:selenium.webdriver.common.selenium_manager:Browser path: /usr/bin/firefox
DEBUG:selenium.webdriver.common.service:Started executable: `/sbin/geckodriver` in a child process with pid: 95813 using 0 to output -3
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:34131/session {'capabilities': {'firstMatch': [{}], 'alwaysMatch': {'browserName': 'firefox', 'acceptInsecureCerts': True, 'moz:debuggerAddress': True, 'pageLoadStrategy': <PageLoadStrategy.normal: 'normal'>, 'browserVersion': None, 'moz:firefoxOptions': {'binary': '/usr/bin/firefox', 'prefs': {'remote.active-protocols': 3}}}}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:34131
DEBUG:urllib3.connectionpool:http://localhost:34131 "POST /session HTTP/1.1" 200 0
DEBUG:selenium.webdriver.remote.remote_connection:Remote response: status=200 | data={"value":{"sessionId":"52486506-ca73-4c4d-b01c-ad7e8f59eee0","capabilities":{"acceptInsecureCerts":true,"browserName":"firefox","browserVersion":"134.0","moz:accessibilityChecks":false,"moz:buildID":"20250106205921","moz:debuggerAddress":"127.0.0.1:46979","moz:geckodriverVersion":"0.35.0","moz:headless":false,"moz:platformVersion":"6.12.9-arch1-1","moz:processID":95820,"moz:profile":"/tmp/rust_mozprofileQC76Mz","moz:shutdownTimeout":60000,"moz:webdriverClick":true,"moz:windowless":false,"pageLoadStrategy":"normal","platformName":"linux","proxy":{},"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify","userAgent":"Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0"}}} | headers=HTTPHeaderDict({'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache', 'content-length': '802', 'date': 'Wed, 29 Jan 2025 15:25:31 GMT'})
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:34131/session/52486506-ca73-4c4d-b01c-ad7e8f59eee0/url {'url': 'http://www.python.org'}
DEBUG:urllib3.connectionpool:http://localhost:34131 "POST /session/52486506-ca73-4c4d-b01c-ad7e8f59eee0/url HTTP/1.1" 200 0
DEBUG:selenium.webdriver.remote.remote_connection:Remote response: status=200 | data={"value":null} | headers=HTTPHeaderDict({'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache', 'content-length': '14', 'date': 'Wed, 29 Jan 2025 15:25:33 GMT'})
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:DELETE http://localhost:34131/session/52486506-ca73-4c4d-b01c-ad7e8f59eee0 {}
DEBUG:urllib3.connectionpool:http://localhost:34131 "DELETE /session/52486506-ca73-4c4d-b01c-ad7e8f59eee0 HTTP/1.1" 200 0
DEBUG:selenium.webdriver.remote.remote_connection:Remote response: status=200 | data={"value":null} | headers=HTTPHeaderDict({'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache', 'content-length': '14', 'date': 'Wed, 29 Jan 2025 15:25:33 GMT'})
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
finish
python3 main.py 4.40s user 1.64s system 4% cpu 2:18.79 totalOperating System
ArchLinux
Selenium version
python 4.28.1
What are the browser(s) and version(s) where you see this issue?
Mozilla Firefox 134.0
What are the browser driver(s) and version(s) where you see this issue?
0.35.0
Are you using Selenium Grid?
No response