Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dba590b

Browse files
committed
Add a few comments around buffer disposal
1 parent d98082d commit dba590b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/libraries/Common/src/System/Net/ArrayBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void Dispose()
5959
byte[] array = _bytes;
6060
_bytes = null!;
6161

62-
if (array != null)
62+
if (array is not null)
6363
{
6464
ReturnBufferIfPooled(array);
6565
}

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ internal sealed partial class Http2Connection : HttpConnectionBase
2929
private readonly Stream _stream;
3030

3131
// NOTE: These are mutable structs; do not make these readonly.
32+
// ProcessIncomingFramesAsync and ProcessOutgoingFramesAsync are responsible for disposing/returning their respective buffers.
3233
private ArrayBuffer _incomingBuffer;
3334
private ArrayBuffer _outgoingBuffer;
3435

@@ -245,6 +246,9 @@ public async ValueTask SetupAsync(CancellationToken cancellationToken)
245246
}
246247
catch (Exception e)
247248
{
249+
// ProcessIncomingFramesAsync and ProcessOutgoingFramesAsync are responsible for disposing/returning their respective buffers.
250+
// SetupAsync is the exception as it's responsible for starting the ProcessOutgoingFramesAsync loop.
251+
// As we're about to throw and ProcessOutgoingFramesAsync will never be called, we must return the buffer here.
248252
_outgoingBuffer.Dispose();
249253

250254
Dispose();
@@ -1886,6 +1890,10 @@ private void FinalTeardown()
18861890
_connectionWindow.Dispose();
18871891
_writeChannel.Writer.Complete();
18881892

1893+
// We're not disposing the _incomingBuffer and _outgoingBuffer here as they may still be in use by
1894+
// ProcessIncomingFramesAsync and ProcessOutgoingFramesAsync respectively, and those methods are
1895+
// responsible for returning the buffers.
1896+
18891897
if (HttpTelemetry.Log.IsEnabled())
18901898
{
18911899
if (Interlocked.Exchange(ref _markedByTelemetryStatus, TelemetryStatus_Closed) == TelemetryStatus_Opened)

0 commit comments

Comments
 (0)