Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve disposal logic in AsyncWebsocketMessageResultEnumerator to pr…
…event 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]>
  • Loading branch information
2 people authored and jsquire committed Jul 10, 2025
commit 5e05f1a3ede34f73d72661db448a8d3b75a6a9fe
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<byte>.Shared.Return(_receiveBuffer);
ArrayPool<byte>.Shared.Return(toReturn);
}
return new ValueTask(Task.CompletedTask);
_webSocket?.Dispose();
return default;
}

public async ValueTask<bool> MoveNextAsync()
Expand Down