Replies: 1 comment
-
In the AsyncState libraries, IAsyncState.RegisterAsyncContext is called only by the AsyncContext<T> constructor. AsyncContext<T> is an internal class and it is registered in DI as an open generic singleton implementation of the IAsyncContext<> and IAsyncLocalContext<> services. So if your application uses IAsyncState.RegisterAsyncContext only in that way, and it resolves IAsyncContext<T> for a bounded set of types, then _contextCount will no longer increase after the DI container has initialised all the AsyncContext<T> instances. If however your application also calls IAsyncState.RegisterAsyncContext directly and an unbounded number of times, then it will leak the AsyncStateToken slots. I think the documentation of IAsyncState.RegisterAsyncContext should be changed to warn against doing so. |
Beta Was this translation helpful? Give feedback.
-
I've noticed a potential memory leak in
Microsoft.Extensions.AsyncState
.The issue seems to be caused by
_contextCount
, which is a static field that is continuously incremented byRegisterAsyncContext()
.Here’s the problem:
_contextCount
is never reset, even whenReset()
is called._contextCount
is used as theAsyncStateToken.Index
, which in turn is used as the index forAsyncStateHolder.Features
.EnsureCount()
ensures thatFeatures
grows to accommodate the largest token index, but since_contextCount
is always increasing,Features
keeps expanding without reusing lower indexes.Features
, which can result in a memory leak.Could you confirm if this is a bug?
(Translated with GPT)
Beta Was this translation helpful? Give feedback.
All reactions