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().
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 implementIDisposable
, 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 inOnCloseAsync()
andOnAbort()
, 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 callsOnCloseAsync()
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 the
IDisposable
interface, call theDispose()
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()
andOnAbort()
.