Skip to content

Commit fdfaad5

Browse files
committed
fb
1 parent 9ae6a5b commit fdfaad5

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/Servers/Kestrel/Core/src/Internal/PinnedBlockMemoryPoolFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public MemoryPool<byte> Create()
2727

2828
_pools.TryAdd(pool, pool);
2929

30-
pool.DisposeCallback = (self) =>
30+
pool.OnPoolDisposed(static (state, self) =>
3131
{
32-
_pools.TryRemove(self, out _);
33-
};
32+
((ConcurrentDictionary<PinnedBlockMemoryPool, PinnedBlockMemoryPool>)state!).TryRemove(self, out _);
33+
}, _pools);
3434

3535
return pool;
3636
}

src/Shared/Buffers.MemoryPool/DefaultMemoryPoolFactory.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ namespace System.Buffers;
1010

1111
#nullable enable
1212

13-
internal sealed class DefaultMemoryPoolFactory : IMemoryPoolFactory<byte>, IDisposable
13+
internal sealed class DefaultMemoryPoolFactory : IMemoryPoolFactory<byte>, IAsyncDisposable
1414
{
1515
private readonly IMeterFactory _meterFactory;
1616
private readonly ConcurrentDictionary<PinnedBlockMemoryPool, PinnedBlockMemoryPool> _pools = new();
1717
private readonly PeriodicTimer _timer;
18+
private readonly Task _timerTask;
1819

1920
public DefaultMemoryPoolFactory(IMeterFactory? meterFactory = null)
2021
{
2122
_meterFactory = meterFactory ?? NoopMeterFactory.Instance;
2223
_timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
23-
_ = Task.Run(async () =>
24+
_timerTask = Task.Run(async () =>
2425
{
2526
try
2627
{
@@ -45,17 +46,18 @@ public MemoryPool<byte> Create()
4546

4647
_pools.TryAdd(pool, pool);
4748

48-
pool.DisposeCallback = (self) =>
49+
pool.OnPoolDisposed(static (state, self) =>
4950
{
50-
_pools.TryRemove(self, out _);
51-
};
51+
((ConcurrentDictionary<PinnedBlockMemoryPool, PinnedBlockMemoryPool>)state!).TryRemove(self, out _);
52+
}, _pools);
5253

5354
return pool;
5455
}
5556

56-
public void Dispose()
57+
public async ValueTask DisposeAsync()
5758
{
5859
_timer.Dispose();
60+
await _timerTask;
5961
}
6062

6163
private sealed class NoopMeterFactory : IMeterFactory

src/Shared/Buffers.MemoryPool/PinnedBlockMemoryPool.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Concurrent;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Diagnostics.Metrics;
78

89
#nullable enable
@@ -30,11 +31,6 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
3031
/// </summary>
3132
public static int BlockSize => _blockSize;
3233

33-
/// <summary>
34-
/// Optional callback to call when pool is being disposed.
35-
/// </summary>
36-
public Action<PinnedBlockMemoryPool>? DisposeCallback { get; set; }
37-
3834
/// <summary>
3935
/// Thread-safe collection of blocks which are currently in the pool. A slab will pre-allocate all of the block tracking objects
4036
/// and add them to this collection. When memory is requested it is taken from here first, and when it is returned it is re-added.
@@ -57,6 +53,9 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
5753

5854
private readonly object _disposeSync = new object();
5955

56+
private Action<object?, PinnedBlockMemoryPool>? _onPoolDisposed;
57+
private object? _onPoolDisposedState;
58+
6059
/// <summary>
6160
/// This default value passed in to Rent to use the default value for the pool.
6261
/// </summary>
@@ -72,6 +71,15 @@ public PinnedBlockMemoryPool(IMeterFactory meterFactory)
7271
_metrics = new(meterFactory);
7372
}
7473

74+
/// <summary>
75+
/// Register a callback to call when the pool is being disposed.
76+
/// </summary>
77+
public void OnPoolDisposed(Action<object?, PinnedBlockMemoryPool> onPoolDisposed, object? state = null)
78+
{
79+
_onPoolDisposed = onPoolDisposed;
80+
_onPoolDisposedState = state;
81+
}
82+
7583
public override IMemoryOwner<byte> Rent(int size = AnySize)
7684
{
7785
if (size > _blockSize)
@@ -214,7 +222,7 @@ protected override void Dispose(bool disposing)
214222
{
215223
_isDisposed = true;
216224

217-
DisposeCallback?.Invoke(this);
225+
_onPoolDisposed?.Invoke(_onPoolDisposedState, this);
218226

219227
if (disposing)
220228
{

0 commit comments

Comments
 (0)