diff --git a/dotnet/src/webdriver/DevTools/WebSocketConnection.cs b/dotnet/src/webdriver/DevTools/WebSocketConnection.cs index 5d21b0d24d8bd..7eb315c25e409 100644 --- a/dotnet/src/webdriver/DevTools/WebSocketConnection.cs +++ b/dotnet/src/webdriver/DevTools/WebSocketConnection.cs @@ -37,6 +37,7 @@ public class WebSocketConnection private Task dataReceiveTask; private bool isActive = false; private ClientWebSocket client = new ClientWebSocket(); + private readonly SemaphoreSlim sendMethodSemaphore = new SemaphoreSlim(1, 1); /// /// Initializes a new instance of the class. @@ -159,7 +160,17 @@ public virtual async Task SendData(string data) { ArraySegment messageBuffer = new ArraySegment(Encoding.UTF8.GetBytes(data)); this.Log($"SEND >>> {data}"); - await this.client.SendAsync(messageBuffer, WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None); + + await sendMethodSemaphore.WaitAsync().ConfigureAwait(false); + + try + { + await this.client.SendAsync(messageBuffer, WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None); + } + finally + { + sendMethodSemaphore.Release(); + } } ///