Polly: [Bug]: Pipeline disposed and cannot be used anymore
Describe the bug
When my application shuts down I wait for 10 seconds for ongoing requests, background jobs, etc. to finish up. During this time, I get the following error:
error.class:"System.ObjectDisposedException"
error.message:"This resilience pipeline has been disposed and cannot be used anymore.
Object name: 'ResiliencePipeline'."
error.stack:" at Polly.Utils.Pipeline.ComponentDisposeHelper.EnsureNotDisposed()
at Polly.ResiliencePipeline.InitializeAsyncContext[TResult](ResilienceContext context)
at Polly.ResiliencePipeline.ExecuteOutcomeAsync[TResult,TState](Func`3 callback, ResilienceContext context, TState state)
at Microsoft.Extensions.Http.Resilience.Internal.ResilienceHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
Expected behavior
The pipeline shouldn’t be disposed if the application is still using it
Actual behavior
The pipeline is disposed while the application is shutting down, and ongoing requests fail
Steps to reproduce
No response
Exception(s) (if any)
No response
Polly version
8.3.0, Microsoft.Extensions.Http.Resilience 8.1.0
.NET Version
mcr.microsoft.com/dotnet/aspnet:8.0
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (9 by maintainers)
You could, but @martintmk works on both so he can chime in on where the issue might be, but if you did create a second issue there’s a good chance you’ll still get asked for a repro that can be used to definitively answer whose problem it is to resolve.
@martintmk Thanks for the detailed reply. I’m using Lamar as my container, perhaps it’s some interaction between the two.
btw, this was only happening when my application was restarting heavily due to some other performance issues. Since resolving those, I haven’t seen this before. I’m guessing there was some “shut down immediately after starting up” or “shut down while gazillions of http requests are going” type issue that was causing this. Now that it’s not happening at all anymore, I’m not planning on spending time digging into it further.
Again, thanks for the assistance here - I appreciate it, even though we didn’t find the root cause, it was helpful to brainstorm a bit.
Hey @alexrosenfeld10
You can only get
ObjectDisposedExceptionfrom the pipeline when it’s owner (ResiliencePipelineRegistry) is disposed. The disposal of registry can happen only when the DI container is disposed, unless you are doing something odd such as disposing it manually before that.My theory why this can be happening is that some service registered in the DI container is using the pipeline in it’s dispose method. If the container disposes the registry before the service is disposed, it’s disposal will fail.
I tried to confirm my theory, but couldn’t replicate it:
At this point we need a repro code to proceed. I suspect you might be doing something unusual that I am not able to track down. But maybe there is indeed some bug in somewhere, if so my bet is that it is outside of Polly.
I’m happy to invest in creating a repro (haven’t tried yet, happy to, just been slammed with work and sick at the same time). Let’s see what Martin (you’re both Martin it appears, I meant @martintmk as you mentioned) has to say here and then go forward from there.