Skip to content

Commit ed831b0

Browse files
committed
Clear the buffer and buffer list before returning to the pool
- This cleans up dumps as the pooled senders don't see references to buffers while pooled in the queue
1 parent 8f254d4 commit ed831b0

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketSender.cs

+14-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
1313
{
1414
internal sealed class SocketSender : SocketAwaitableEventArgs
1515
{
16+
private List<ArraySegment<byte>>? _bufferList;
17+
1618
public SocketSender(PipeScheduler scheduler) : base(scheduler)
1719
{
1820
}
@@ -24,11 +26,6 @@ public SocketAwaitableEventArgs SendAsync(Socket socket, in ReadOnlySequence<byt
2426
return SendAsync(socket, buffers.First);
2527
}
2628

27-
if (!MemoryBuffer.Equals(Memory<byte>.Empty))
28-
{
29-
SetBuffer(null, 0, 0);
30-
}
31-
3229
SetBufferList(buffers);
3330

3431
if (!socket.SendAsync(this))
@@ -41,19 +38,18 @@ public SocketAwaitableEventArgs SendAsync(Socket socket, in ReadOnlySequence<byt
4138

4239
public void Reset()
4340
{
44-
// TODO: Consider clearing the buffer and buffer list before we put it back into the pool
45-
// it's a performance hit but it removes the confusion when looking at dumps to see this still
41+
// We clear the buffer and buffer list before we put it back into the pool
42+
// it's a small performance hit but it removes the confusion when looking at dumps to see this still
4643
// holder onto the buffer when it's back in the pool
44+
BufferList = null;
45+
46+
SetBuffer(null, 0, 0);
47+
48+
_bufferList?.Clear();
4749
}
4850

4951
private SocketAwaitableEventArgs SendAsync(Socket socket, ReadOnlyMemory<byte> memory)
5052
{
51-
// The BufferList getter is much less expensive then the setter.
52-
if (BufferList != null)
53-
{
54-
BufferList = null;
55-
}
56-
5753
SetBuffer(MemoryMarshal.AsMemory(memory));
5854

5955
if (!socket.SendAsync(this))
@@ -69,25 +65,23 @@ private void SetBufferList(in ReadOnlySequence<byte> buffer)
6965
Debug.Assert(!buffer.IsEmpty);
7066
Debug.Assert(!buffer.IsSingleSegment);
7167

72-
var buffers = BufferList;
73-
74-
if (buffers == null)
68+
if (_bufferList == null)
7569
{
76-
buffers = new List<ArraySegment<byte>>();
70+
_bufferList = new List<ArraySegment<byte>>();
7771
}
7872
else
7973
{
8074
// Buffers are pooled, so it's OK to root them until the next multi-buffer write.
81-
buffers.Clear();
75+
_bufferList.Clear();
8276
}
8377

8478
foreach (var b in buffer)
8579
{
86-
buffers.Add(b.GetArray());
80+
_bufferList.Add(b.GetArray());
8781
}
8882

8983
// The act of setting this list, sets the buffers in the internal buffer list
90-
BufferList = buffers;
84+
BufferList = _bufferList;
9185
}
9286
}
9387
}

0 commit comments

Comments
 (0)