Skip to content

fix(handler): keep an uncoalesced cache fill alive after the client hangs up - #456

Open
ravi-pplx wants to merge 1 commit into
block:mainfrom
perplexityai:fix/handler-fill-after-hangup
Open

ravi-pplx wants to merge 1 commit into
block:mainfrom
perplexityai:fix/handler-fill-after-hangup

Conversation

@ravi-pplx

@ravi-pplx ravi-pplx commented Nov 21, 2026 •

Copy link
Copy Markdown

Coalesced misses already fill the cache from a detached context. Misses that aren't coalesced still create the cache writer with the request context: Range and conditional requests, and misses the spool can't share. Every cache backend checks that context when the write commits, and an S3 commit also waits for the upload and the metadata write. A client that has the whole body usually closes its connection during that wait, which cancels the request, so:

  • the entry is discarded and the next request misses again;
  • the handler logs Failed to fetch and cache with context canceled;
  • cachew.cache.fills records the fill as canceled.

Change

  • The cache writer gets a context that keeps the request's values and survives its cancellation for up to 30 seconds. It is released when the handler returns.
  • The origin request still uses the request context, so a client that leaves before the body is complete still aborts both the fetch and the cache write.
  • A fill that commits after the client left is recorded as success.

Testing

  • TestUncoalescedFillSurvivesClientHangup sends a conditional miss through a real HTTP server, with a cache whose commit is held until the server has seen the client hang up. When the client reads the full body, the entry is stored and the fill counts as a success. When it reads half while the origin is still sending, nothing is stored.
  • TestDetachedFillContextGrace checks the 30-second bound.
  • Manual check against MinIO with the S3 cache: of 20 conditional misses where the client read the full body and closed its connection, none were stored before this change and all 20 after.

Created with Perplexity

…angs up

Misses that are not coalesced (Range and conditional requests, and
misses the spool cannot share) create the cache writer with the request
context. Every cache backend checks that context when the write
commits, and an S3 commit waits for the upload and metadata write. A
client that has the whole body usually hangs up during that wait, so
the entry is discarded and the fill is counted as canceled.

The writer now uses a context that keeps the request's values and
outlives its cancellation by up to 30 seconds. The origin request is
still request-scoped, so a client that leaves mid-body still aborts the
write.

Signed-off-by: Ravi Agarwal <ravi@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
@ravi-pplx
ravi-pplx requested a review from a team as a code owner November 21, 2026 21:28
@ravi-pplx
ravi-pplx requested review from worstell and removed request for a team November 21, 2026 21:28
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T21:32:03.458394Z c2eb476 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant