Skip to content

Commit

Permalink
[dotnet] Be more defensive when shutting down BiDi WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Aug 31, 2023
1 parent 05c148f commit 0d0eb4a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
17 changes: 2 additions & 15 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Expand Up @@ -55,8 +55,6 @@ public class DevToolsSession : IDevToolsSession

private DevToolsDomains domains;

private readonly SemaphoreSlim semaphoreSlimForSocketSend = new SemaphoreSlim(1, 1);

/// <summary>
/// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint.
/// </summary>
Expand Down Expand Up @@ -219,18 +217,7 @@ public async Task<JToken> SendCommand(string commandName, JToken commandParamete

string contents = JsonConvert.SerializeObject(message);
this.pendingCommands.TryAdd(message.CommandId, message);

// socket SendAsync cannot be ran simultaneously, waiting available single worker
await semaphoreSlimForSocketSend.WaitAsync(cancellationToken);

try
{
await this.connection.SendData(contents);
}
finally
{
semaphoreSlimForSocketSend.Release();
}
await this.connection.SendData(contents);

var responseWasReceived = await Task.Run(() => message.SyncEvent.Wait(millisecondsTimeout.Value, cancellationToken));

Expand Down Expand Up @@ -335,7 +322,7 @@ protected void Dispose(bool disposing)
{
this.Domains.Target.TargetDetached -= this.OnTargetDetached;
this.pendingCommands.Clear();
this.TerminateSocketConnection().GetAwaiter().GetResult();
Task.Run(async () => await this.TerminateSocketConnection()).GetAwaiter().GetResult();
}

this.isDisposed = true;
Expand Down
4 changes: 4 additions & 0 deletions dotnet/src/webdriver/DevTools/WebSocketConnection.cs
Expand Up @@ -186,6 +186,10 @@ protected virtual async Task CloseClientWebSocket()
{
// An OperationCanceledException is normal upon task/token cancellation, so disregard it
}
catch (WebSocketException e)
{
this.Log($"Unexpected error during attempt at close: {e.Message}", DevToolsSessionLogLevel.Error);
}
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions dotnet/test/common/DevTools/DevToolsTestFixture.cs
Expand Up @@ -39,6 +39,7 @@ public void Teardown()
{
session.Dispose();
EnvironmentManager.Instance.CloseCurrentDriver();
session = null;
driver = null;
}
}
Expand Down

0 comments on commit 0d0eb4a

Please sign in to comment.