From 294bd7081c5a7419981717ea05ff9cfcc6de5e83 Mon Sep 17 00:00:00 2001 From: Bharath Kumar Bellam <82457244+Bellambharath@users.noreply.github.com> Date: Mon, 1 Jun 2026 19:07:08 +0530 Subject: [PATCH] Fix OutputCache caching truncated responses for aborted requests When a request is canceled (RequestAborted), OutputCacheMiddleware could store an empty or truncated response in the cache if: 1. No Content-Length header is present (e.g. removed by ResponseCompression) 2. A decorator stream above OutputCacheStream throws before the write reaches OutputCacheStream, so BufferingEnabled stays true Fix: - In FinalizeCacheBodyAsync, skip storing if RequestAborted is canceled - In ExecuteResponseAsync (locking path), return null if the request was aborted so that waiting concurrent requests are not served a truncated response via TryServeCachedResponseAsync Fixes #66877 --- .../src/OutputCacheMiddleware.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs b/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs index 695250703208..bfa2997decdc 100644 --- a/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs +++ b/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs @@ -171,6 +171,16 @@ private async Task InvokeAwaited(HttpContext httpContext, IReadOnlyList