microsoft / service-fabric-services-and-actors-dotnet

Reliable Services and Reliable Actors are Service Fabric application frameworks for building highly-scalable distributed cloud applications.
Other
269 stars 113 forks source link

Service Fabric support for IDisposable Stateful/Stateless Services #354

Open besseb05 opened 1 year ago

besseb05 commented 1 year ago

Problem We are working with Service Fabric services in production, and our services create native resources which implement IDisposable. In order to dispose of these resources correctly, we made our services also implement IDisposable, but Service Fabric currently doesn't support disposing of the service objects, which has led to a buildup of resources. We have added logic for disposing of the resources in OnCloseAsync() and OnAbort(), but this has still caused issues.

In particular, we noticed this problem when due to some issues, our service failed to start up and repeatedly threw exceptions in the OnOpenAsync() method. When this happens, the Service Fabric runtime never calls OnCloseAsync() on the service object, and doesn't dispose it either before creating a new instance. After a short while, since the resources we were allocating never got disposed, it caused the memory and thread count of our service to increase substantially.

Proposed solution At the end of the lifetime of the service object, if the service implements theIDisposable interface, call the Dispose() method on it. This will help ensure services which allocate native resources will be able to reliably clean them up.

Alternatives considered As mentioned above, we have tried to ensure that these resources are always cleaned up by disposing of them in the OnCloseAsync() and OnAbort().