You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As described .NET documentation, developers expect that services (and function instances) should be disposed even when they only implement IAsyncDisposable. In fact, IServiceProvider
created from Microsoft.Extensions.DependencyInjection.ServiceCollection handles IAsyncDisposable, but Azure Function runtime does not. It leads resource leaks caused by objects which only implement IAsyncDisposable, including ServiceBusSender and ServiceBusReceiver of the Azure ServcieBus, they eventually causes receive link exhausion.
This issue is same as #2870, which was closed as "completed" but it was not solved.
Repro steps
Implement IAsyncDisposable for your function class or your service class.
Run function. DisposeAsync() of function isntances, transient services, and scoped services never be called.
Shutdown function. DisposeAsync() of singleton services never be called.
Expected behavior
If functions and services implement IAsyncDisposable, their DisposeAsync() will be called in end of their lifetime.
If they also implement IDisposable, IAsyncDisposable.DisposeAsync() should be preferred.
Actual behavior
DisposeAsync() never be called.
Known workarounds
Avoid to store IAsyncDisposable objects to fields of function instances and their dependencies.
Note that singleton is not workaround because their DisposeAsync() should be called in graceful shutdown.
Use isolated worker for services. As far as I saw, isolated worker never dispose service instance even it implement IDisposable, so it is incomplete workaround.
As described .NET documentation, developers expect that services (and function instances) should be disposed even when they only implement
IAsyncDisposable
. In fact,IServiceProvider
created from
Microsoft.Extensions.DependencyInjection.ServiceCollection
handlesIAsyncDisposable
, but Azure Function runtime does not. It leads resource leaks caused by objects which only implementIAsyncDisposable
, includingServiceBusSender
andServiceBusReceiver
of the Azure ServcieBus, they eventually causes receive link exhausion.This issue is same as #2870, which was closed as "completed" but it was not solved.
Repro steps
IAsyncDisposable
for your function class or your service class.DisposeAsync()
of function isntances, transient services, and scoped services never be called.DisposeAsync()
of singleton services never be called.Expected behavior
If functions and services implement
IAsyncDisposable
, theirDisposeAsync()
will be called in end of their lifetime.If they also implement
IDisposable
,IAsyncDisposable.DisposeAsync()
should be preferred.Actual behavior
DisposeAsync()
never be called.Known workarounds
IAsyncDisposable
objects to fields of function instances and their dependencies.DisposeAsync()
should be called in graceful shutdown.IDisposable
, so it is incomplete workaround.Related information
As far as I saw, following methods should be fixed. I am ready to create PR if my assumption is right.
IAsyncDisposable.DisposeAsync
to:*Scope
* Also fix
TrackDisposable
to recordIAsyncDisposable
AsyncDispose()
in:IAsyncDisposable.DisposeAsync()
methods described above.There are many DI related classes in azure-functions-host repo, so it is OK to post issue if needed.
The text was updated successfully, but these errors were encountered: