Edi.RouteDebugger icon indicating copy to clipboard operation
Edi.RouteDebugger copied to clipboard

Cannot Access A Closed Stream in app.UseExceptionHandler when Route Debugger is enabled

Open wwarby opened this issue 2 years ago • 1 comments

I hit an issue this morning which I've tracked down to this route debugger package. I no longer actually need the route debugger so I'll be removing the package from my project this morning, just letting you know what happened.

In Startup.cs public void Configure, I have an exception handler, followed further down by the route debugger.

app.UseExceptionHandler(a => a.Run(async context => {
   await context.Response.WriteAsync("anything"); // <-- throws `Cannot access a closed stream`
});
...
if (env.IsDevelopment()) { app.UseRouteDebugger(); } // <-- Commenting out this line makes the problem go away

The way I invoked an exception to cause the exception handler to be called was simply by throwing a new Exception on the first line of a controller action.

I'm not sure exactly why this happens and I'm afraid I don't have the time at to dig any further into it, but I'm pretty sure you must have something in UseRouteDebugger that is latching on to the HttpContext's response object and finalizing it's stream in such a way as to prevent other middleware from manipulating it. I tried, clearing the response, finalizing and flushing it myself, nothing worked.

wwarby avatar Nov 11 '21 11:11 wwarby