Skip to content

Commit 58e297d

Browse files
Merge pull request #1353 from botinko/concurrentBagLeak
Fix memory leak regression
2 parents 0362f7e + 885ed44 commit 58e297d

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/Autofac/Core/Registration/DefaultRegisteredServicesTracker.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,7 @@ protected override void Dispose(bool disposing)
249249
registration.Dispose();
250250
}
251251

252-
// If we do not explicitly empty the ConcurrentBag that stores our registrations,
253-
// this will cause a memory leak due to threads holding a reference to the bag.
254-
// In netstandard2.0 the faster 'Clear' method is not available,
255-
// so we have do this manually. We'll use the faster method if it's available though.
256-
#if NETSTANDARD2_0
257-
while (_registrations.TryTake(out _))
258-
{
259-
}
260-
#else
261-
_registrations.Clear();
262-
#endif
252+
ClearRegistrations();
263253

264254
base.Dispose(disposing);
265255
}
@@ -272,9 +262,26 @@ protected override async ValueTask DisposeAsync(bool disposing)
272262
await registration.DisposeAsync().ConfigureAwait(false);
273263
}
274264

265+
ClearRegistrations();
266+
275267
// Do not call the base, otherwise the standard Dispose will fire.
276268
}
277269

270+
private void ClearRegistrations()
271+
{
272+
// If we do not explicitly empty the ConcurrentBag that stores our registrations,
273+
// this will cause a memory leak due to threads holding a reference to the bag.
274+
// In netstandard2.0 the faster 'Clear' method is not available,
275+
// so we have do this manually. We'll use the faster method if it's available though.
276+
#if NETSTANDARD2_0
277+
while (_registrations.TryTake(out _))
278+
{
279+
}
280+
#else
281+
_registrations.Clear();
282+
#endif
283+
}
284+
278285
private ServiceRegistrationInfo GetInitializedServiceInfo(Service service)
279286
{
280287
var createdEphemeralSet = false;

0 commit comments

Comments
 (0)