Skip to content

[🐛 Bug]: Ensuring Drivers close #15634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
titusfortner opened this issue Apr 16, 2025 · 5 comments
Open

[🐛 Bug]: Ensuring Drivers close #15634

titusfortner opened this issue Apr 16, 2025 · 5 comments
Labels
C-dotnet .NET Bindings C-java Java Bindings C-nodejs JavaScript Bindings C-rb Ruby Bindings I-defect Something is not working as intended

Comments

@titusfortner
Copy link
Member

titusfortner commented Apr 16, 2025

Description

Selenium closes the driver as part of the quit method, but it is possible for Selenium to start the driver, but error when creating the session, which prevents the driver process from being closed.

I see this for sure in Ruby, but I think this applies to other languages as well, so we should check.

Reproducible Code

n/a

Debugging Logs

n/a
@titusfortner titusfortner added C-dotnet .NET Bindings C-java Java Bindings C-nodejs JavaScript Bindings C-py Python Bindings C-rb Ruby Bindings I-defect Something is not working as intended labels Apr 16, 2025
@titusfortner titusfortner marked this as a duplicate of #15632 Apr 16, 2025
@cgoldberg
Copy link
Contributor

Python handles this correctly. If you try to instantiate a browser and the session is not created, it will raise SessionNotCreatedException and stop the driver process.

For example, the following code will raise an exception, stop execution, and no chromedriver processes will remain:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--user-data-dir=/no/such/location")
driver = webdriver.Chrome(options=options)

@titusfortner
Copy link
Member Author

self.service.stop() is only called from the quit() method of each driver class. If the driver isn't instantiated, quit can never be called, and that method isn't explicitly executed. It's possible the system might kill child processes when the python process ends? Actually, I should figure out why that isn't happening in Ruby. But I think this is the issue with #15632 and I think it makes sense to explicitly call self.service.stop() when the self.start_session(capabilities) has a session creation error.

@cgoldberg
Copy link
Contributor

It's possible the system might kill child processes when the python process ends?

Not the system, but our code is killing the child processes when the Python interpreter exits.

There is a __del__ method in the selenium.webdriver.common.service.Service class that all drivers use. When the Python interpreter exits, it calls __dell__ on every object. This method calls self.stop(), just like driver.quit() does, so all the child processes get killed.

I think it makes sense to explicitly call self.service.stop() when the self.start_session(capabilities) has a session creation error.

Now that I think about it more, that makes sense. If you continuously create drivers, and handle the exception without exiting the program (like a test framework might do), the drivers wouldn't get cleaned up until the end.

I'll see if I can add that.

@cgoldberg
Copy link
Contributor

I think this is all that is needed for Python: #15636

@cgoldberg
Copy link
Contributor

This is completed for Python via #15636

@titusfortner titusfortner removed the C-py Python Bindings label Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-dotnet .NET Bindings C-java Java Bindings C-nodejs JavaScript Bindings C-rb Ruby Bindings I-defect Something is not working as intended
Projects
None yet
Development

No branches or pull requests

2 participants