socket.io-client-csharp icon indicating copy to clipboard operation
socket.io-client-csharp copied to clipboard

Emit deadlock under heavy load from asp.net due to missing synch. context negation (ConfigureAwait(false))

Open logicmill opened this issue 6 months ago • 0 comments

WebsocketTransport.SendAsync (line 168) continuation not executing on same thread due to missing ConfigureAwait(false) which causes deadlocking on _sendLock.WaitAsync.

        public override async Task SendAsync(IList<SerializedItem> items, CancellationToken cancellationToken)
        {
            try
            {
                await _sendLock.WaitAsync(cancellationToken).ConfigureAwait(false);

                if (items[0].Type == SerializedMessageType.Text)
                {
                    var bytes = Encoding.UTF8.GetBytes(items[0].Text);

                    ///// This causes a deadlock on _sendLock.WaitAsync above
                    //await SendAsync(TransportMessageType.Text, bytes, cancellationToken);
                    /////// Should be
                    await SendAsync(TransportMessageType.Text, bytes, cancellationToken).ConfigureAwait(false);
                    ///////

                    Debug.WriteLine($"[WebSocket⬆] {items[0].Text}");
                }

                var binary = items.AllBinary();
                if (binary.Count > 0)
                {
                    foreach (var b in binary)
                    {
                        await SendAsync(TransportMessageType.Binary, b, cancellationToken).ConfigureAwait(false);
                    }

                    Debug.WriteLine($"[WebSocket⬆]0️⃣1️⃣0️⃣1️⃣ x {binary.Count}");
                }
            }
            finally
            {
                _sendLock.Release();
            }
        }

logicmill avatar Jun 10 '25 22:06 logicmill