System.ObjectDisposedException: IFeatureCollection has been disposed.

See original GitHub issue

Describe the bug

A few .NET Core applications running in the Azure Service Fabric cluster are crashing several times per day with the exception: System.ObjectDisposedException: IFeatureCollection has been disposed.

To Reproduce

Unfortunately, I was not able to reproduce the issue on my machine. I suspect that might be caused by IHttpContextAccessor use in a singleton class. I am not capturing the HttpContext in a private field, nor using async void anywhere. I have ruled out all the bad practices mentioned in the guide by @davidfowl:

https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md#do-not-store-ihttpcontextaccessorhttpcontext-in-a-field

Exceptions (if any)

Application: service.exe
CoreCLR Version: 4.700.20.41105
.NET Core Version: 3.1.8
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException: IFeatureCollection has been disposed.
Object name: 'Collection'.
   at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ThrowContextDisposed()
   at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Scheme()
   at Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(HttpRequest request)
   at IFBaltics.Hosting.WebHostRequestFiller.Fill(LogEventInfo logEventInfo)
   at Sysmsg.SysmsgLogCommand.Write()
   at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)
   at Microsoft.AspNetCore.Server.HttpSys.DisconnectListener.<>c__DisplayClass7_0.<CreateDisconnectToken>b__0(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPtr)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pNativeOverlapped)

Further technical details

  • ASP.NET Core version 3.1.8

  • It was not possible to find any installed .NET Core SDKs Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from: https://aka.ms/dotnet-download

    Host (useful for support): Version: 3.1.8 Commit: 9c1330dedd

    .NET Core SDKs installed: No SDKs were found.

    .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.21 [c:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.21 [c:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.8 [c:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.21 [c:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.8 [c:\Program Files\dotnet\shared\Microsoft.NETCore.App]

    To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download

  • Not dependant on the IDE.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Sep 21, 2020

It’s a combination of things:

  • I’m assuming you have a custom logger that is adding properties from the request to log entries
  • We’re creating a cancellation token which ends up capturing the execution context (including the HttpContextAccessor) and logging after the request is over.
  • The custom logger tries to access the HttpContext fields and throws as a result.

We need to avoid capturing the ExecutionContext here https://github.com/dotnet/aspnetcore/blob/d8caeb2e2f9ef942b3fbd346c992ce648ed3f008/src/Servers/HttpSys/src/NativeInterop/DisconnectListener.cs#L70

0reactions
davidfowlcommented, Oct 9, 2020

Are you using httpsys server? That’s what this specific issues was about.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IFeatureCollection has been disposed when trying to ...
Create a REST client that sends many requests simultaneously. The result is that eventually, a System.ObjectDisposedException will be thrown ...
Read more >
IFeatureCollection has been disposed. in Asp.net Core ...
Scoped services are disposed at the request's end. The main problem is OnChanged can be called after the request's end.
Read more >
IFeatures Collection has been disposed, object Name
Hi Dear All, i have created a web api project and created a new web api named login, now i created asp.net core...
Read more >
IFeatureCollection has been disposed when trying to ...
IFeatureCollection has been disposed when trying to access HttpContext.Request.Headers dotnet/AspNetCore #20551.
Read more >
ObjectDisposedException on ASP.NET Core
ERROR System.ObjectDisposedException: The collection has been disposed. Object name: 'BlockingCollection'. at System.Collections.Concurrent.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found