Skip to content
15 changes: 14 additions & 1 deletion dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ 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 @@ -217,7 +219,18 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains

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

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

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

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

Expand Down