diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index 1d6466c2436fa..33d98824665c7 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsSession.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsSession.cs @@ -55,6 +55,8 @@ public class DevToolsSession : IDevToolsSession private DevToolsDomains domains; + private readonly SemaphoreSlim semaphoreSlimForSocketSend = new SemaphoreSlim(1, 1); + /// /// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint. /// @@ -217,7 +219,18 @@ public T GetVersionSpecificDomains() 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));