DataDog / dd-trace-dotnet

.NET Client Library for Datadog APM
https://docs.datadoghq.com/tracing/
Apache License 2.0
409 stars 131 forks source link

AspNetCore OutputCacheMiddleware throws NullReferenceException from BlockingMIddleware #5440

Open aviram-zuzut-glbe opened 1 month ago

aviram-zuzut-glbe commented 1 month ago

I added OutputCache attributes to a few controllers in my service, and I see this issue occurs a lot since then -

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNetCore.OutputCaching.OutputCacheMiddleware.TryServeCachedResponseAsync(OutputCacheContext context, OutputCacheEntry cacheEntry, IReadOnlyList1 policies) at Microsoft.AspNetCore.OutputCaching.OutputCacheMiddleware.InvokeAwaited(HttpContext httpContext, IReadOnlyList1 policies) at Datadog.Trace.ClrProfiler.AutoInstrumentation.AspNetCore.BlockingMiddleware.Invoke(HttpContext context) in /project/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AspNetCore/BlockingMiddleware.cs:line 140 at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Datadog.Trace.ClrProfiler.AutoInstrumentation.AspNetCore.BlockingMiddleware.Invoke(HttpContext context) in /project/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AspNetCore/BlockingMiddleware.cs:line 140 at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

To Reproduce Add OutputCache attribute to an endpoint example - [OutputCache(PolicyName = "DefaultCachePolicy", Duration = 300, VaryByQueryKeys = new[] { "*" }, VaryByHeaderNames = new[] { "Accept-Language" })]

Expected behavior I will not see this issue appear.

Runtime environment (please complete the following information):

anna-git commented 1 month ago

Hi there,

We can't seem to reproduce the issue with the OutputCache attribute. Datadog.Trace.ClrProfiler.AutoInstrumentation.AspNetCore.BlockingMiddleware is expected to appear in the call stack in any case, which doesn't mean it's responsible for the exception thrown. Besides, the OutputCacheMiddleware already caused NullReferenceExceptions in the past, as we can see on the last fix for it.

Could you try running the application without the tracer and see if the exception is still happening?

aviram-zuzut-glbe commented 1 month ago

Hey Anna, thanks for replying! I checked and the errors are thrown only by DataDog tracer(I have dozens of calls per day, the issue does not appear besides DataDog). Also, I already updated my .net version to 7.0.17-bullseye-slim, this version should cover this issue already.

anna-git commented 1 month ago

Hi again, I would suggest you contact support directly at https://www.datadoghq.com/support/ so we can fetch all the necessary details and go through your case. Thank you