Skip to content

fix: join detached cache work before embedded storage closes - #256

Merged
ovaistariq merged 12 commits into
mainfrom
perfloop-pr-open-rvg0nzpqgf
Oct 21, 2026
Merged

ovaistariq merged 12 commits into
mainfrom
perfloop-pr-open-rvg0nzpqgf

Conversation

@perfloop

@perfloop perfloop Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

proxy.Service.Stop now seals detached-work admission, cancels the Service lifetime, and joins admitted cache users after HTTP handlers drain but before main closes embedded storage. The Service lifetime is independent of an individual request, so a cache warm can continue after a client disconnect; shutdown cancels and joins it. Full-object writes have their own registered owner, so a fetch-supervisor timeout cannot hide a still-running PutStream or release its reservation early. An admitted tiered cleanup retains its timeout through the conditional S3 delete and marker repair. Shutdown still joins it because cancellation after dispatch can leave the remote result ambiguous.

TestShutdownWaitsForBackgroundWriterBeforeEmbeddedClose runs TAG as a child process with embedded storage and a controlled local S3 origin. It holds an admitted writer through SIGTERM and records admission, cancellation, writer exit, and backend close. A retained result-mapping driver also runs the cold-range process after the fetch supervisor times out and the active-fetch count returns to zero, plus a separate active-writer control. TestShutdownDrainsNormalRequestWithinGrace checks an ordinary request completing within the existing HTTP grace. TestStopWaitsForWriterAfterFetchSupervisorTimesOut observes the reservation until the writer exits, and TestTieredCleanupFinishesMarkerRepairDuringServiceStop covers the joined conditional-delete path. The existing TestColdOwner_RangeWarmsCacheOnClientCancel integration test remains in the suite.

The same process-and-reservation assertion is violated on test-only comparison 84ae52583b59e4299e6bf23bb354c59be853f5c3 and satisfied on the final source.

make build, make test, make test-integration, go test -race -count=1 ./proxy, and make lint-ci passed. The process and result-mapping drivers are retained verification support, not part of this PR.

No production shutdown incidence or overall shutdown duration was measured. The HTTP drain keeps its 30-second deadline; the worker join has none, and an uncooperative cache operation can outlast the bundled StatefulSet's 30-second termination grace. No fixed total completion time is promised. This ordering applies after successful HTTP draining, not to handlers that survive its deadline. See graceful shutdown.


Generated by Perfloop.


Note

Medium Risk
Changes core shutdown and background-cache ownership across many hot paths; mis-ordering could still corrupt embedded storage or extend shutdown, though coverage is extensive.

Overview
Fixes shutdown ordering so embedded RocksDB storage is not closed while detached cache writers still run.

proxy.Service.Stop (called from main after the HTTP server drain) seals admission to new background work, cancels a service-wide lifetime context, and blocks until every admitted task exits—only then does embeddedCache.Close run. A join past 30 seconds logs a warning with the outstanding task count; there is no hard deadline on the join itself.

Detached work that used bare go func() with context.Background() now goes through startBackgroundTask / beginBackgroundTask, so block fetches, parquet footer warms, background populates, tiered cleanup, write-through tees, and similar paths are tracked and canceled on shutdown. Cache-write supervisors keep populate slots and byte reservations until the real PutStream/PutBlock owner finishes; abandoned pipe readers are closed so relays cannot block Stop.

Docs (README, docs/deploy.md) describe the two-phase shutdown (HTTP drain, then worker join) and the Kubernetes termination-grace caveat.

Tests add subprocess probes for writer-before-close ordering and in-flight HTTP drain, plus unit tests for lifecycle, tiered cleanup during stop, and parquet admission after Stop.

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

@greptile-apps

greptile-apps Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues remain.

Summary

TAG now joins detached cache work before closing embedded storage.

  • TAG joins detached cache work before embedded storage closes.
  • Cache tasks stay tied to the service that owns their storage.
  • Tiered cleanup finishes its marker repair before storage closes.

Both previous unnumbered findings are addressed. No new actionable issues were found. This review inspected code and tests; it did not run them.

Reviews (5) · Last reviewed commit: "Merge origin/main into perfloop-pr-open-..." · Reviewed by Greptile

Comment thread proxy/caching.go Resolved
@perfloop

perfloop Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

On SIGINT or SIGTERM, a cache-enabled TAG process now seals detached-work admission, cancels the Service lifetime, and joins admitted cache users after HTTP handlers drain but before main closes embedded storage. The Service lifetime stays independent of an individual request, so cache warming can continue after a client disconnect; shutdown still cancels and joins it. For foreground full GETs, the cache writer closes its pipe reader on return and passes its error through to the relay. This lets a relay blocked on queued bytes finish when a canceled remote write stops reading. Range-triggered full-object writes retain their own registered owner after fetch-supervisor timeout, keeping their slot and byte reservation until the actual writer exits. Tiered cleanup keeps its timeout through conditional S3 delete and marker repair; Stop still joins it because cancellation after dispatch can leave the remote outcome ambiguous.

TestShutdownWaitsForBackgroundWriterBeforeEmbeddedClose runs TAG as a child process with embedded storage and a controlled local S3 origin. It holds a writer through SIGTERM and records admission, cancellation, writer exit, and backend close. TestServiceStopUnblocksFullGetWhenCacheWriterStopsReading uses a controlled cache client that reads one byte and returns on service cancellation; it verifies the completed 200 response is preserved and Stop joins without a test-side pipe drain. TestShutdownDrainsNormalRequestWithinGrace checks the ordinary drain. TestStopWaitsForWriterAfterFetchSupervisorTimesOut checks that new detached work is rejected while Stop waits on a held writer and that its reservation remains held until exit. TestTieredCleanupFinishesMarkerRepairDuringServiceStop covers the conditional-delete path. The existing client-cancel warming integration test remains. Process and result-mapping drivers are retained verification support, not part of this PR.

The same process, admission, and reservation assertion is violated on test-only comparison 84ae52583b59e4299e6bf23bb354c59be853f5c3 and satisfied on the final source.

The repository checks make build, make test, make test-integration, go test -race -count=1 ./proxy, and make lint-ci passed.

No production shutdown incidence or overall shutdown duration was measured. HTTP draining keeps its existing 30-second deadline; the worker join has no deadline and an uncooperative cache operation can outlast the bundled StatefulSet's 30-second grace. No fixed total completion time is promised. This ordering applies after successful HTTP draining, not to handlers that survive the deadline. See graceful shutdown.


Generated by Perfloop.

perfloop-agent and others added 7 commits October 16, 2026 05:03
Warn once when Stop waits past the drain deadline for in-flight handlers.

Resolve handlers/server.go against #259 by keeping this branch's NewServer
(which already moved the http.Server literal and adds BaseContext) plus
#259's comment. Adapt #259's stop-before-start test to check that the
server handler dispatches to the router instead of comparing identity,
since the router is now wrapped by the handler lifecycle barrier.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
Rebase #256 over #237/#239/#240 and #258: keep main's populateBlockMode
split, RangeBlockSpan publication, lazyCommitWriter range serve and the
32-slot parquet footer prefetch semaphore, with every detached spawn still
admitted through serviceWorkers.

A footer prefetch that serviceWorkers refuses during Stop now releases its
admission slot and dedup marker exactly as a finished scan would, so a
shutdown-time refusal cannot leak one of the 32 permits.

Service.Stop logs one Warn when its unbounded join passes 30s (main's drain
timeout) with the outstanding task count, and one Info with the elapsed
wait when the join then completes, mirroring warnHandlerJoinPastDeadline.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
Comment thread docs/deploy.md Resolved
ovaistariq and others added 2 commits October 21, 2026 10:21
deploy.md described graceful shutdown twice, and the second copy said
handlers surviving the drain deadline were not covered. Server.Stop now
joins in-flight handlers past the deadline with a warning, and
Service.Stop joins detached cache work with its own warning, so fold both
sections into one that states that ordering once.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6

@ovaistariq ovaistariq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed: every detached populate/fetch now runs under a service-lifetime supervisor joined by Service.Stop before embedded storage closes; the prefetch slot is released on shutdown-time refusal; the join warns once past 30s. Rebased on #258/#240/#237/#239.

@ovaistariq
ovaistariq merged commit e21d02c into main Oct 21, 2026
9 checks passed
@ovaistariq
ovaistariq deleted the perfloop-pr-open-rvg0nzpqgf branch October 21, 2026 21:20
@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