Skip to content

Commit 1e5da07

Browse files
jsquirechristothesstephentoub
authored
Improve disposal logic in AsyncWebsocketMessageResultEnumerator to prevent multiple disposals (#527)
* 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]> * Update src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs --------- Co-authored-by: Christopher Scott <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
1 parent 2ae7d06 commit 1e5da07

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/Custom/Realtime/Internal/AsyncWebsocketMessageEnumerator.cs

Lines changed: 4 additions & 5 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,12 +26,11 @@ public AsyncWebsocketMessageResultEnumerator(WebSocket webSocket, CancellationTo
2626

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

3736
public async ValueTask<bool> MoveNextAsync()

0 commit comments

Comments
 (0)