Skip to content

engine/server: sweep discarded cache state in the background - #13916

Merged
vito merged 2 commits into
mainfrom
fix-discarded-cache-sweep
Aug 28, 2026
Merged

vito merged 2 commits into
mainfrom
fix-discarded-cache-sweep

Conversation

@vito

@vito vito commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Resetting invalid local cache state — after an unclean shutdown, a schema
mismatch, a corrupt DB — deleted the entire worker root directory inline with
os.RemoveAll before the engine came up.

On a warm cache that is tens of gigabytes across millions of inodes. Startup
blocked for the whole delete, and the unlink storm saturated the disk queue
hard enough to stutter the host for about a minute. The engine hadn't done
anything yet; it was still taking out the trash.

Rename, then sweep. The discarded state is moved aside to a sibling
worker-trash-<unique> directory — an O(1) rename on the same filesystem — so
startup proceeds immediately. A background sweeper then removes the tree at a
bounded duty cycle: 50ms of unlinks, 200ms of rest, so the filesystem journal
drains between bursts instead of being monopolized.

The sweeper is not naive about it:

  • It runs at full speed when the disk is under the same free-space pressure
    that triggers cache GC — pacing is a courtesy, not a suicide pact.
  • Leftover trash dirs are resumed on the next startup if a sweep was
    interrupted, so nothing accumulates silently.
  • Stale mounts left behind by the crashed engine are detached rather than
    failing the reset (recursively up front, plus an EBUSY fallback per path).

The unclean-shutdown integration test now polls the state volume until the
trash dir is actually swept, so discarded state cannot quietly leak across
resets.

New: engine/server/trash.go and its unit tests. Changelog entry included.


Independent fix, based on main. Extracted from the LLM workspace stack
(#13832–#13838); it has no dependency on those PRs and can land on its own.

@vito
vito force-pushed the fix-discarded-cache-sweep branch from bc0dbe0 to 1e14375 Compare August 18, 2026 16:58

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

non blocking nit for a further corner case, if you agree worth a quick fix otherwise lgtm

Comment thread engine/server/trash.go
Comment on lines +141 to +144
// trashSweepFullSpeed reports whether the sweeper should skip its rest slices:
// once the disk is under the same free-space pressure that triggers cache GC,
// reclaiming the trashed bytes promptly matters more than smoothing I/O.
func (srv *Server) trashSweepFullSpeed() bool {

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.

nit: this makes sense, but in the case where the disk was actually full or nearly full I think we'd need to go further and still block startup on this removal rather than going async. Otherwise the engine will probably just shutdown again quickly once it starts trying to make files and fails

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done via 674d2cd

vito and others added 2 commits August 27, 2026 13:32
Resetting invalid local cache state (unclean shutdown, schema mismatch,
corrupt DBs) deleted the entire worker root dir inline with os.RemoveAll
before the engine came up. On a warm cache that is tens of GB across
millions of inodes: startup blocked for the whole delete, and the unlink
storm saturated the disk queue hard enough to stutter the host for about
a minute.

Rename the discarded state aside to a sibling worker-trash-<unique> dir
instead — an O(1) rename on the same filesystem — so startup proceeds
immediately, and remove the tree from a background sweeper at a bounded
duty cycle (50ms of unlinks, 200ms of rest) so the filesystem journal
drains between bursts instead of monopolizing the disk. The sweeper runs
at full speed when the disk is under the same free-space pressure that
triggers cache GC, resumes leftover trash dirs on the next startup if
interrupted, and detaches stale mounts left behind by the crashed engine
(recursively up front, plus an EBUSY fallback per path) rather than
failing the reset.

The unclean-shutdown integration test now also polls the state volume
until the trash dir is swept, so discarded state cannot silently leak
across resets.

Signed-off-by: Alex Suraci <[email protected]>
Remove discarded worker state synchronously when the configured GC policy
reports disk pressure. This ensures startup reclaims enough space for the
engine to operate before it begins creating files again.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the fix-discarded-cache-sweep branch from 674d2cd to ba4e216 Compare August 27, 2026 17:32
@vito
vito merged commit 3d525f3 into main Aug 28, 2026
89 checks passed
@vito
vito deleted the fix-discarded-cache-sweep branch August 28, 2026 18:33
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.

2 participants