ChromeWebDriver.dispose() does not execute HTTP command after timeout
Meta -
OS: Windows 10
Selenium Version:
"Selenium.WebDriver" version="3.8.0"
"Selenium.WebDriver.ChromeDriver" version="2.34.0"
Browser:
Chrome
Browser Version:
Versjon 64.0.3282.167 (64-bit)
Expected Behavior -
driver.Dispose(); - close open drivers and clean up their processes. This works if no timeout occurs.
Actual Behavior -
chromedriver.exe is registered as an app in Task manager, with two "children":

When driver.dispose() is called after a timeout in the browser, the HTTP command directed to the webdriver is not executed.
"OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:port/session/sessionid/element timed out after 60 seconds.
----> System.Net.WebException : The request was aborted: The operation has timed out."
Only the "chromedriver.exe" app and processchild is killed, leaving the Console window Host an orphan process. However, this orphaned process is holding the chromedriver.exe process, so the next time we run our tests the build folder cannot be cleaned.
Steps to reproduce -
The ReproduceError() test, will as the name says, reproduce the error as long as a breakpoint in fiddler are set: bpu https://google.com and one waits for selenium to timeout. The windows will not close, in the cleanup.
We have a temporary worakaround demonstrated in ErrorWorkAround(), using processid's to kill the process in question (still using a breakpoint in fiddler, and waiting for the timeout to appear).
[Test]
public void ReproduceError()
{
using (var driver = new ChromeDriver())
{
try
{
driver.Navigate().GoToUrl("https://bing.com");
// Set breakpoint in fiddler: bpu https://google.com
// Wait for selenium to timeout
driver.Navigate().GoToUrl("https://google.com");
driver.FindElementById("fkbx-text");
}
finally
{
driver.Dispose();
}
}
}
[Test]
public void ErrorWorkAround()
{
var service = ChromeDriverService.CreateDefaultService();
ChromeDriver driver;
try
{
driver = new ChromeDriver(service);
driver.Navigate().GoToUrl("https://bing.com");
// Set breakpoint in fiddler: bpu https://google.com
// Wait for selenium to timeout
driver.Navigate().GoToUrl("https://google.com");
driver.FindElementById("fkbx-text");
}
finally
{
var pid = service.ProcessId;
Process.GetProcessById(pid).CloseMainWindow();
}
}
This issue is also present with FirefoxDriver.
I'am also having this same issue. help me to get the chrome driver processId using Java. Thanks in Advance @ktakle
Even am also facing the same issue in both chrome and firefox driver.
One thing I noticed is that ChromeDriver and FirefoxDriver in C# doesn't override the Dispose method so it will currently only "Close the Browser and Dispose of WebDriver", could this be the issue?
This also happens when using Python with Chrome.
RemoteWebDriver class have method dispose() contains code
this.Dispose(true);
GC.SuppressFinalize(this);
Dispose(true) internally executes
this.Execute(DriverCommand.Quit, null);
this.StopClient();
this.sessionId = null;
If preferred, I can open a separate issue for this, but this issue seems to be essentially the same as mine, only more specific. The attached .Net solution shows the more general form of this failure.
Specifically a command can timeout, but is not aborted when that happens. It will rather continue running in the background until completion. Any other commands that are queued up in the interim will run a risk of timing out as well. Moreover, a click timeout, for example, is not actually a timeout attempting to click the selected element. This makes it misleading when attempting to troubleshoot a test failure, because the actual timeout was a load timeout.
Many, many help requests for this sort of failure are on Stack Overflow (and a few elsewhere). It took me a week to collect enough information myself to realize what was really going on. I have a workaround now, but it would save many headaches if a command timeout left the webdriver in a state to immediately accept another command rather than having to wait for the timed out one to complete ... which in the case of this solution can be made predictable but in actual tests cannot be.
I left out a piece of the solution that was required to get a successful build when I was trying to make this small enough to upload. This one includes it. You'll still need to do a NuGet package update before it will build, because the other required components are large, but I've confirmed you can get them that way.
This is the process I cobbled together to get a list of orphaned processes left after the webdriver closes. The driverservice used to create the webdriver needs to be accessible to get the PID.
Even if the original process has ended child processes are still marked with their parent process id. Currently I am not checking each process for nested processes.
public void Dispose()
{
IEnumerable<Process> GetChildProcesses(int id)
{
var children = new List<Process>();
var mos = new ManagementObjectSearcher($"Select * From Win32_Process Where ParentProcessID={id}");
foreach (var o in mos.Get())
{
var mo = (ManagementObject) o;
children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
}
return children;
}
var pid = DriverService?.ProcessId ?? 0;
EventWrappedDriver.Dispose();
if (pid > 0)
{
foreach (var process in GetChildProcesses(pid))
{
process.Kill();
}
}
if ((DriverService?.ProcessId ?? 0) != 0)
{
Process.GetProcessById(Convert.ToInt32(DriverService?.ProcessId)).Kill();
}
}
I think something like this could be added to the dispose in DriverServiceCommandExecutor in order to always clean up the child processes.
I think something like this could be added to the dispose in DriverServiceCommandExecutor in order to always clean up the child processes.
It would be better to fix the actual issue than applying a workaround for cleaning up orphaned drivers.
Should this be on https://bugs.chromium.org/p/chromedriver/issues/list rather than here?
Is this still happening with the most recent version of the bindings? (beta-2)
@diemol I believe so based on some results I have seen but let me put together a sample to confirm one way or another. I always saw this happening in cases where the timeout occurred due to chrome getting a basic auth prompt and the chrome.exe would be left open though everything else closed, so I am not sure if that was the same issue.
Were you able to reproduce this, @JDCain?
@diemol It looks like it is corrected. I used v4-beta.2 and used this test to confirm it closes the browser process even if it prompt comes up. It appears the prompt no longer causes a timeout. I don't know another way to force the wedriver to timeout in order to see if it closes after the timeout.
public class TestClass
{
[Fact]
public void BasicPromptHangTest()
{
var options = new ChromeOptions();
options.AddArgument("--headless");
var service =
ChromeDriverService.CreateDefaultService(Path.Combine(Directory.GetCurrentDirectory(), "WebDrivers"));
var driver = new ChromeDriver(service, options);
var process = Process.GetProcessById(service.ProcessId);
Process[] children = GetChildProcesses(service.ProcessId).ToArray();
driver.Navigate().GoToUrl("https://jigsaw.w3.org/HTTP/Basic/");
driver.Dispose();
Assert.All(children, p => Assert.True(p.HasExited));
Assert.True(process.HasExited);
}
private static IEnumerable<Process> GetChildProcesses(int id)
{
var children = new List<Process>();
using var mos = new ManagementObjectSearcher($"Select * From Win32_Process Where ParentProcessID={id}");
foreach (ManagementBaseObject o in mos.Get())
try
{
var mo = (ManagementObject)o;
var process = Process.GetProcessById(Convert.ToInt32(mo["ProcessID"]));
children.Add(process);
}
catch (Exception)
{
//ignore
}
return children;
}
}
Thank you for replying, @JDCain! I will close this for now.