SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.6k stars 8.17k forks source link

[🐛 Bug]: C#: Driver process left running if DriverService is reused and Driver disposed #14624

Open kevinoid opened 1 week ago

kevinoid commented 1 week ago

What happened?

Using the same instance of ChromeDriverService to construct a second ChromeDriver instance after a first has been disposed causes a chromedriver.exe process to remain running after the program exits. The same issue occurs for EdgeDriverService/EdgeDriver (and presumably others, although I haven't tested).

How can we reproduce the issue?

var driverService = ChromeDriverService.CreateDefaultService();
var driver1 = new ChromeDriver(driverService);
driver1.Dispose();
var driver2 = new ChromeDriver(driverService);
driver2.Dispose();
### Relevant log output ```shell 18:12:02.680 TRACE SeleniumManager: Driver path: C:\Users\Kevin\.cache\selenium\chromedriver\win64\130.0.6723.58\chromedriver.exe 18:12:02.684 TRACE SeleniumManager: Browser path: C:\Program Files\Google\Chrome\Application\chrome.exe Starting ChromeDriver 130.0.6723.58 (3a50e012e4c9b8a410a4e2b12bf577e69ee8f755-refs/branch-heads/6723@{#1353}) on port 56805 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully on port 56805. 18:12:03.024 DEBUG HttpCommandExecutor: Executing command: []: newSession {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"binary":"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"}}]}} 18:12:03.049 TRACE HttpCommandExecutor: >> Method: POST, RequestUri: 'http://localhost:56805/session', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers: { Accept: application/json; charset=utf-8 User-Agent: selenium/4.25.0 User-Agent: (.net windows) Content-Type: application/json; charset=utf-8 } {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"binary":"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"}}]}} DevTools listening on ws://127.0.0.1:56808/devtools/browser/69268b21-d6f4-4d17-adc8-0bb82d03c81f 18:12:09.128 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: no-cache Content-Length: 880 Content-Type: application/json; charset=utf-8 } 18:12:09.229 DEBUG HttpCommandExecutor: Response: (18ded7c147cc833ccfa3021204277f05 Success: System.Collections.Generic.Dictionary`2[System.String,System.Object]) 18:12:09.673 DEBUG HttpCommandExecutor: Executing command: [18ded7c147cc833ccfa3021204277f05]: quit {} 18:12:09.768 TRACE HttpCommandExecutor: >> Method: DELETE, RequestUri: 'http://localhost:56805/session/18ded7c147cc833ccfa3021204277f05', Version: 1.1, Content: , Headers: { User-Agent: selenium/4.25.0 User-Agent: (.net windows) Accept: application/json Accept: image/png } 18:12:12.719 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: no-cache Content-Length: 14 Content-Type: application/json; charset=utf-8 } 18:12:12.720 DEBUG HttpCommandExecutor: Response: ( Success: ) Starting ChromeDriver 130.0.6723.58 (3a50e012e4c9b8a410a4e2b12bf577e69ee8f755-refs/branch-heads/6723@{#1353}) on port 56805 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully on port 56805. 18:12:14.313 DEBUG HttpCommandExecutor: Executing command: []: newSession {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{}}]}} 18:12:14.322 TRACE HttpCommandExecutor: >> Method: POST, RequestUri: 'http://localhost:56805/session', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers: { Accept: application/json; charset=utf-8 User-Agent: selenium/4.25.0 User-Agent: (.net windows) Content-Type: application/json; charset=utf-8 } {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{}}]}} DevTools listening on ws://127.0.0.1:56818/devtools/browser/c557c74a-4deb-4032-af34-de1cb7df9c9f 18:12:17.843 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: no-cache Content-Length: 882 Content-Type: application/json; charset=utf-8 } 18:12:17.864 DEBUG HttpCommandExecutor: Response: (fbc0e9deb4d2f1929299573b6f0c8826 Success: System.Collections.Generic.Dictionary`2[System.String,System.Object]) 18:12:18.098 DEBUG HttpCommandExecutor: Executing command: [fbc0e9deb4d2f1929299573b6f0c8826]: quit {} 18:12:18.147 TRACE HttpCommandExecutor: >> Method: DELETE, RequestUri: 'http://localhost:56805/session/fbc0e9deb4d2f1929299573b6f0c8826', Version: 1.1, Content: , Headers: { User-Agent: selenium/4.25.0 User-Agent: (.net windows) Accept: application/json Accept: image/png } 18:12:21.467 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: no-cache Content-Length: 14 Content-Type: application/json; charset=utf-8 } 18:12:21.468 DEBUG HttpCommandExecutor: Response: ( Success: ) ```

Operating System

Windows 10

Selenium version

C# with Selenium.WebDriver 4.25.0

What are the browser(s) and version(s) where you see this issue?

Chrome 130.0.6723.59

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 130.0.6723.58

Are you using Selenium Grid?

No

github-actions[bot] commented 1 week ago

@kevinoid, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

kevinoid commented 1 week ago

If it is incorrect to reuse an instance of DriverService, perhaps it would make sense for the Driver constructor to throw, or for DriverService to throw instead of starting a new driver service process from a disposed instance?

nvborisenko commented 1 week ago

How I am reading it:

var driverService = ChromeDriverService.CreateDefaultService(); // just new driverService object
var driver1 = new ChromeDriver(driverService); 
driver1.Dispose(); // you dispose driver1 but not driverService 
var driver2 = new ChromeDriver(driverService);
driver2.Dispose(); // you dispose driver2 but not driverService 

So, driverService should not be disposed! And it is correct behavior.

kevinoid commented 1 week ago

So, driverService should not be disposed! And it is correct behavior.

Are you saying it is a bug that driverService is disposed by driver1.Dispose()? For reference, this occurs in WebDriver.Dispose(), which calls DriverServiceCommandExecutor.Dispose(): https://github.com/SeleniumHQ/selenium/blob/d922168d801e5a468ae3edbc68cb1055f04c9f41/dotnet/src/webdriver/WebDriver.cs#L731 which calls DriverService.Dispose(): https://github.com/SeleniumHQ/selenium/blob/d922168d801e5a468ae3edbc68cb1055f04c9f41/dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs#L156

nvborisenko commented 6 days ago

How it works currently:

var driverService = ChromeDriverService.CreateDefaultService();
var driver1 = new ChromeDriver(driverService); // starts new chromedriver.exe
driver1.Dispose(); // ends the chromedriver.exe
var driver2 = new ChromeDriver(driverService); // starts new chromedriver.exe
driver2.Dispose(); // DOES NOT end the chromedriver.exe

This is definitely a bug, user experience should be improved here. I don't know whether ChromeDriverService is supposed to be reusable: understanding it let's us understand what is expected user experience should be.

@diemol @titusfortner do you know whether ChromeDriverService is considered as reusable object, comparing with other bindings?

nvborisenko commented 6 days ago

@kevinoid did it work properly with previous Selenium version, and which one?

kevinoid commented 6 days ago

did it work properly with previous Selenium version, and which one?

@nvborisenko Testing just now, I'm able to reproduce the issue with Selenium.WebDriver versions back to 3.11.1, but I'm unable to reproduce the issue with 3.11.0 or earlier.

nvborisenko commented 6 days ago

Thank you good man, it will help so much!

nvborisenko commented 3 days ago

Current understanding is: ChromeDriverService should be reusable.