AspNetCore.CacheOutput icon indicating copy to clipboard operation
AspNetCore.CacheOutput copied to clipboard

Empty byte array cached on request timeout

Open nazar554 opened this issue 5 years ago • 4 comments

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:

  1. Start app in debug mode
  2. Place a breakpoint inside action
  3. Call action in Swagger UI
  4. Wait until Promise fails with timeout (depends on browser/Swagger UI defaults)
  5. Click continue inside VS
  6. Now retry the request, from now on you will always get an empty response (content-length is zero) until cache expires

Suggestions:

  • Either document that one should always use cancellationToken.ThrowIfCancellationRequested() inside actions or risk invalid data
  • Or handle CancellationToken somewhere inside [CacheOutput] action filter

Environment: .NET 6, Windows 10

nazar554 avatar Nov 15 '21 19:11 nazar554

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 avatar Nov 24 '21 14:11 Iamcerba

@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.

nazar554 avatar Nov 24 '21 15:11 nazar554

I was able to reproduce the issue only in .NET 6. context.HttpContext.Response.Body whenever the request was canceled.

Iamcerba avatar Nov 24 '21 16:11 Iamcerba

@nazar554 I've created PR (https://github.com/Iamcerba/AspNetCore.CacheOutput/pull/27). Please validate.

Iamcerba avatar Nov 24 '21 16:11 Iamcerba