Skip to content
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

[dotnet] Add dispose on constructor failure to ensure driver closes #13673

Merged
merged 7 commits into from
Mar 15, 2024
24 changes: 21 additions & 3 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds
protected WebDriver(ICommandExecutor executor, ICapabilities capabilities)
{
this.executor = executor;
this.StartSession(capabilities);

try
{
this.StartSession(capabilities);
}
catch (Exception)
{
// Failed to start driver session, disposing of driver
this.DriverDispose();
MASACR99 marked this conversation as resolved.
Show resolved Hide resolved
throw;
}

this.elementFactory = new WebElementFactory(this);
this.network = new NetworkManager(this);
this.registeredCommands.AddRange(DriverCommand.KnownCommands);
Expand Down Expand Up @@ -674,6 +685,15 @@ protected bool RegisterInternalDriverCommand(string commandName, CommandInfo com
/// </summary>
/// <param name="disposing">if its in the process of disposing</param>
protected virtual void Dispose(bool disposing)
{
this.DriverDispose();
this.executor.Dispose();
}

/// <summary>
/// Send Quit command to stop driver
/// </summary>
protected virtual void DriverDispose()
{
try
{
Expand All @@ -692,8 +712,6 @@ protected virtual void Dispose(bool disposing)
{
this.sessionId = null;
}

this.executor.Dispose();
}

private static void UnpackAndThrowOnError(Response errorResponse, string commandToExecute)
Expand Down