Skip to content

fix: prevent partial whole-object cache entries - #263

Merged
ovaistariq merged 6 commits into
mainfrom
perfloop-pr-open-a2gqqxxqq9
Oct 21, 2026
Merged

ovaistariq merged 6 commits into
mainfrom
perfloop-pr-open-a2gqqxxqq9

Conversation

@perfloop

@perfloop perfloop Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

A full-object GET miss streams the origin body to the client and a bounded cache listener. If the listener queue fills or the upstream body read fails, losing the terminal error can make the stream look complete to the cache and publish a prefix under full-object metadata.

The broadcaster now reserves a queue slot for the terminal error by returning one displaced pooled buffer before closing a failed listener. Clean completion keeps all queued data. The cache pipe wraps EOF-shaped terminal failures so the embedded store cannot mistake io.ErrUnexpectedEOF for a normal short inline value. The README and cache-control guide describe the behavior; no public API, configuration, or schema changes.

The new routed TestBroadcastErrorsDoNotPublishIncompleteWholeObject was added with this change. It fails on the test-only comparison because both terminal-error paths leave an incomplete entry visible, and passes on final; it checks the cache state, a complete client response on the slow-cache path, and the later healthy GET. The new control TestBroadcastCompletionKeepsFullQueuedBody passes on both the test-only comparison and final. The original TestBroadcasterSlowConsumerDisconnect passed on base while allowing a clean close; its tightened version failed on the test-only comparison with nil instead of ErrSlowConsumer and passes on final with the terminal error. Existing TestCacheableFullGet_SubsequentRequestHitsCache, TestServeAssembledRange_RemoteMissRejectsShortValidatedBlock, and TestETagVersionedBody_VersionsCoexistUntilTTL passed in the scoped suites. The routed test uses an in-memory cache fixture that mirrors the pinned embedded store's inline handling of io.ErrUnexpectedEOF.

The proxy, handler, and cache package tests, the proxy/handler race run, make lint-ci, and make build passed. The full repository make test and make test-integration suites were not run here; they remain target-CI work. The comparison runner is not included in the pull request; the native tests it runs are. Production frequency has not been measured.

This condition holds with this change and does not hold at 41d4f6941b3e18d7ac53ffdd8a0dafc1b94be943: For TAG's cacheable whole-object HTTP GET misses with an unchanged metadata version token, both a slow-listener drop and an upstream body-read error must make the cache write fail and must not publish a partial body under complete-object metadata; a healthy client still receives the full body when only its cache listener is dropped, and the next healthy GET returns the full body..


Generated by Perfloop.


Note

Medium Risk
Changes correctness-sensitive whole-object cache populate and broadcast disconnect semantics; wrong handling could still serve truncated responses to slow clients or skip caching when healthy.

Overview
Fixes a bug where whole-object cache populate could commit metadata for a truncated body when the broadcast stream ended badly but looked like a clean EOF.

Broadcaster: Adds Listener.Err() and closeWithError, which always records the terminal result on the listener and only sends an error chunk when the queue has room—queued data chunks are never dropped or reordered (replacing behavior that could swap in an error chunk and skip bytes). Cache and GET handlers consult Err() after the chunk channel closes when no error chunk was delivered (e.g. slow-consumer disconnect with a full buffer).

Cache populate path: Treats that terminal error like an in-stream failure, wraps EOF-shaped errors via cachePipeReadError so inline storage cannot treat io.ErrUnexpectedEOF as a normal short value, and PutWithMetaStreamIfVersion refuses to write metadata unless streamed bytes match Content-Length (ErrBodyLengthMismatch).

Docs/tests: README and docs/cache-control.md state that failed cache listeners or upstream body errors do not publish partial entries; integration-style handler tests and unit tests cover slow listener, upstream failure, full-queue success, and length mismatch.

Reviewed by Cursor Bugbot for commit 3daad57. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issues were found.

Summary

The PR prevents failed whole-object broadcasts from looking like successful cache writes.

  • Failed whole-object streams no longer become visible cache entries.
  • The benchmark fixture note now describes its shared test role.

No actionable new issues were found. No previous Greptile threads were supplied.

Reviews (2) · Last reviewed commit: "fix(broadcast): surface listener errors ..." · Reviewed by Greptile

ovaistariq and others added 2 commits October 20, 2026 23:43
Conflicts resolved: README.md keeps both appended lines; broadcaster.go
Complete() uses the PR's closeWithError with main's b.err; caching.go
keeps the PR's cachePipeReadError wrapping alongside main's relayErr branch.
PR #263 fixed two real bugs: a slow-consumer disconnect dropped its
ErrSlowConsumer on the floor when the listener's buffer was full (so the
cache listener drained to a clean close and committed a truncated body
under the full Content-Length), and an upstream mid-body drop surfaced as
io.ErrUnexpectedEOF, which the ocache backend accepted as "small value,
done". The cachePipeReadError wrapping for the second bug is kept as is.

Its fix for the first bug was unsafe: closeWithError made room for the
error chunk by receiving one queued chunk. Channels are FIFO, so that
removed the OLDEST unread chunk, and a consumer that had read chunks
0..k-1 next received k+1 at k's offset - wrong bytes on the wire before
the error arrived.

Replace the displacement with a terminal-error field on Listener:

- closeWithError now records the error in Listener.termErr (written
  under Broadcaster.mu before close(ch)), tries a best-effort
  non-blocking send of Chunk{Err: err}, and closes the channel. Queued
  data chunks are never dropped or reordered.
- Listener.Err() exposes the terminal error. It is valid once Chunks()
  has closed: the close happens-before the receive that observes it, so
  consumers read it without extra locking after their range loop ends.
- Both consumers honour it. writeChunksToResponse (client path) returns
  Err() after the loop exactly as it would an error chunk, so a
  disconnected client never gets a clean EOF with a short body.
  setupCacheListener (cache path) sets chunkErr from Err() when the loop
  ended on a clean close, so the pipe fails and no metadata is committed.
- Subscribe's already-done path records termErr too, for consistency.

Defense in depth: PutWithMetaStreamIfVersion now counts the bytes the
store consumed and refuses the meta commit with ErrBodyLengthMismatch
when they differ from meta.ContentLength (short or long), mirroring the
io.ReadFull guard in putBlocksFromStream. This closes the root cause
behind #251 and #249 even if some future producer loses the error
signal. The versioned body is left to age out via TTL, as with a lost
precondition.

Tests: the slow-consumer test now reads Err() after the loop (the error
chunk is best-effort on a full queue); new broadcaster tests assert that
a full-buffer disconnect delivers every queued chunk in order with no
hole and then surfaces the error via Err(), and that a failed Complete
on a full queue does the same; new cache tests assert a short or long
body is not committed and an exact or unknown length still is. The PR's
handler-level tests pass unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
ovaistariq added a commit that referenced this pull request Oct 21, 2026
Route every broadcast disconnect through Listener.closeWithError: the
disconnectSlowConsumer helper used by both the copying Broadcast path and
the ownership-transferring broadcastOwnedLocked path now records termErr,
queues a best-effort error chunk, and closes the channel without displacing
queued data. Complete() keeps #260's headerCh/headerErr behaviour and #263's
closeWithError completion. On the owned path a chunk rejected by a full
owner queue is never queued, so the caller keeps and releases that buffer;
a chunk already queued stays with its listener and is released by the
consumer's drain. Adds a test pinning that owned-path disconnect contract.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
@ovaistariq
ovaistariq merged commit 355803f into main Oct 21, 2026
9 of 10 checks passed
@ovaistariq
ovaistariq deleted the perfloop-pr-open-a2gqqxxqq9 branch October 21, 2026 18:36
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 21, 2026
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.22.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants