Skip to content

Commit c57f430

Browse files
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 <[email protected]> --------- Co-authored-by: Stephen Toub <[email protected]>
1 parent 01d6d76 commit c57f430

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/Custom/RealtimeConversation/Internal/AsyncWebsocketMessageEnumerator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal partial class AsyncWebsocketMessageResultEnumerator : IAsyncEnumerator<
1313
public ClientResult Current { get; private set; }
1414
private readonly CancellationToken _cancellationToken;
1515
private readonly WebSocket _webSocket;
16-
private readonly byte[] _receiveBuffer;
16+
private byte[] _receiveBuffer;
1717

1818
public AsyncWebsocketMessageResultEnumerator(WebSocket webSocket, CancellationToken cancellationToken)
1919
{
@@ -26,9 +26,12 @@ public AsyncWebsocketMessageResultEnumerator(WebSocket webSocket, CancellationTo
2626

2727
public ValueTask DisposeAsync()
2828
{
29-
ArrayPool<byte>.Shared.Return(_receiveBuffer);
29+
if (Interlocked.Exchange(ref _receiveBuffer, null) is byte[] toReturn)
30+
{
31+
ArrayPool<byte>.Shared.Return(toReturn);
32+
}
3033
_webSocket?.Dispose();
31-
return new ValueTask(Task.CompletedTask);
34+
return default;
3235
}
3336

3437
public async ValueTask<bool> MoveNextAsync()

0 commit comments

Comments
 (0)