Skip to content

Commit 0d0eb4a

Browse files
committed
[dotnet] Be more defensive when shutting down BiDi WebSocket
1 parent 05c148f commit 0d0eb4a

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

dotnet/src/webdriver/DevTools/DevToolsSession.cs

+2-15
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class DevToolsSession : IDevToolsSession
5555

5656
private DevToolsDomains domains;
5757

58-
private readonly SemaphoreSlim semaphoreSlimForSocketSend = new SemaphoreSlim(1, 1);
59-
6058
/// <summary>
6159
/// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint.
6260
/// </summary>
@@ -219,18 +217,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
219217

220218
string contents = JsonConvert.SerializeObject(message);
221219
this.pendingCommands.TryAdd(message.CommandId, message);
222-
223-
// socket SendAsync cannot be ran simultaneously, waiting available single worker
224-
await semaphoreSlimForSocketSend.WaitAsync(cancellationToken);
225-
226-
try
227-
{
228-
await this.connection.SendData(contents);
229-
}
230-
finally
231-
{
232-
semaphoreSlimForSocketSend.Release();
233-
}
220+
await this.connection.SendData(contents);
234221

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

@@ -335,7 +322,7 @@ protected void Dispose(bool disposing)
335322
{
336323
this.Domains.Target.TargetDetached -= this.OnTargetDetached;
337324
this.pendingCommands.Clear();
338-
this.TerminateSocketConnection().GetAwaiter().GetResult();
325+
Task.Run(async () => await this.TerminateSocketConnection()).GetAwaiter().GetResult();
339326
}
340327

341328
this.isDisposed = true;

dotnet/src/webdriver/DevTools/WebSocketConnection.cs

+4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ protected virtual async Task CloseClientWebSocket()
186186
{
187187
// An OperationCanceledException is normal upon task/token cancellation, so disregard it
188188
}
189+
catch (WebSocketException e)
190+
{
191+
this.Log($"Unexpected error during attempt at close: {e.Message}", DevToolsSessionLogLevel.Error);
192+
}
189193
}
190194

191195
/// <summary>

dotnet/test/common/DevTools/DevToolsTestFixture.cs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void Teardown()
3939
{
4040
session.Dispose();
4141
EnvironmentManager.Instance.CloseCurrentDriver();
42+
session = null;
4243
driver = null;
4344
}
4445
}

0 commit comments

Comments
 (0)