From 5e05f1a3ede34f73d72661db448a8d3b75a6a9fe Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Tue, 24 Jun 2025 11:43:52 -0500 Subject: [PATCH 1/2] Improve disposal logic in AsyncWebsocketMessageResultEnumerator to prevent multiple disposals (#476) * Improve disposal logic in AsyncWebsocketMessageResultEnumerator to prevent multiple disposals * fb * Update src/Custom/RealtimeConversation/Internal/AsyncWebsocketMessageEnumerator.cs Co-authored-by: Stephen Toub --------- Co-authored-by: Stephen Toub --- .../Internal/AsyncWebsocketMessageEnumerator.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs b/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs index 4e581dc31..7f48e2f4d 100644 --- a/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs +++ b/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs @@ -13,7 +13,7 @@ internal partial class AsyncWebsocketMessageResultEnumerator : IAsyncEnumerator< public ClientResult Current { get; private set; } private readonly CancellationToken _cancellationToken; private readonly WebSocket _webSocket; - private readonly byte[] _receiveBuffer; + private byte[] _receiveBuffer; public AsyncWebsocketMessageResultEnumerator(WebSocket webSocket, CancellationToken cancellationToken) { @@ -26,12 +26,12 @@ public AsyncWebsocketMessageResultEnumerator(WebSocket webSocket, CancellationTo public ValueTask DisposeAsync() { - _webSocket?.Dispose(); - if (_receiveBuffer is not null) + if (Interlocked.Exchange(ref _receiveBuffer, null) is byte[] toReturn) { - ArrayPool.Shared.Return(_receiveBuffer); + ArrayPool.Shared.Return(toReturn); } - return new ValueTask(Task.CompletedTask); + _webSocket?.Dispose(); + return default; } public async ValueTask MoveNextAsync() From e11a9e960f8ee4f4c19e44a938bba89fff58096e Mon Sep 17 00:00:00 2001 From: Jesse Squire Date: Thu, 10 Jul 2025 11:28:42 -0700 Subject: [PATCH 2/2] Update src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs --- src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs b/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs index 7f48e2f4d..d5c8d8ea8 100644 --- a/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs +++ b/src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs @@ -30,7 +30,6 @@ public ValueTask DisposeAsync() { ArrayPool.Shared.Return(toReturn); } - _webSocket?.Dispose(); return default; }