Empty byte array cached on request timeout
Using [CacheOutput] inside an action which doesn't handle CancellationToken stores a byte[0] inside cache.
Subsequent requests return 304 Not Modified if used with Cache-Control: no-cache.
Steps to reproduce:
- Start app in debug mode
- Place a breakpoint inside action
- Call action in Swagger UI
- Wait until Promise fails with timeout (depends on browser/Swagger UI defaults)
- Click continue inside VS
- Now retry the request, from now on you will always get an empty response (
content-lengthis zero) until cache expires
Suggestions:
- Either document that one should always use
cancellationToken.ThrowIfCancellationRequested()inside actions or risk invalid data - Or handle
CancellationTokensomewhere inside[CacheOutput]action filter
Environment: .NET 6, Windows 10
Hi @nazar554,
Can I ask you to provide a viable example? I can't reproduce.
As I understand you are explicitly specifying "CancellationToken cancellationToken" as the controller's argument (without handling cancellation in the code) and cancel inflight requests on the client side. If yes, the backend should fully process the request anyway unless ThrowIfCancellationRequested / IsCancellationRequested is used as if it was a regular request... so I don't see any problems.
@Iamcerba
It works fine if the action calls ThrowIfCancellationRequested. The problem occurs when the action doesn't receive CancellationToken as a parameter.
For some reason if request was cancelled (but no exception was thrown, because action doesn't use CancellationToken), an empty byte array still gets cached, and its served for subsequent requests.
I was able to reproduce the issue only in .NET 6. context.HttpContext.Response.Body whenever the request was canceled.
@nazar554 I've created PR (https://github.com/Iamcerba/AspNetCore.CacheOutput/pull/27). Please validate.