assetcache: keep bytes of unremoved temporaries counted; Abort can retry
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2 files changed,
+120
−24
internal/assetcache/fill.go+63 −23internal/assetcache/fill_test.go+57 −1
CI failed — run details
diff --git a/internal/assetcache/fill.go b/internal/assetcache/fill.go| index b321a25a2..c2afff79a 100644 |
| --- a/internal/assetcache/fill.go |
| +++ b/internal/assetcache/fill.go |
| @@ -63,8 +63,16 @@ type Fill struct { |
| tmpName string |
| token string // names every temporary of this fill |
| written int64 |
| - failed error // set by the first failed write |
| - done bool // committed or aborted: the reservation is released |
| + failed error // set by the first failed write |
| + done bool // committed or aborted: the fill slot is released |
| + held int64 // bytes of the reservation still counted in s.reserved |
| + left []leftover // temporaries an Abort could not remove yet |
| +} |
| + |
| +// leftover is a temporary still on disk and the bytes it holds. |
| +type leftover struct { |
| + path string |
| + size int64 |
| } |
| // Reserve admits a fill for k, or says why not. contentLength is the length |
| @@ -99,7 +107,7 @@ func (s *Store) Reserve(k Key, contentLength int64) (*Fill, error) { |
| if err := s.admit(scope, res.total(), total, free); err != nil { |
| return nil, err |
| } |
| - f := &Fill{s: s, key: k, scope: scope, id: k.ID(), res: res} |
| + f := &Fill{s: s, key: k, scope: scope, id: k.ID(), res: res, held: res.total()} |
| if err := f.open(); err != nil { |
| f.finish() |
| return nil, err |
| @@ -191,41 +199,73 @@ func (f *Fill) Write(p []byte) (int, error) { |
| } |
| // Abort deletes the fill's temporaries and releases its reservation and fill |
| -// slot. It is safe to call twice and after Commit. |
| +// slot. It is safe to call twice and after Commit. If a temporary cannot be |
| +// removed, the fill slot is still released but the bytes of the files that |
| +// remain stay counted as reserved, so the budget and the floor see them; a |
| +// later Abort retries the removal, and Open removes them at the next start. |
| func (f *Fill) Abort() { |
| f.mu.Lock() |
| defer f.mu.Unlock() |
| - if f.done { |
| - return |
| - } |
| f.finish() |
| } |
| -// finish closes and removes the temporaries, then releases the reservation |
| -// and the slot, once. The caller holds f.mu, or has not shared f. |
| +// finish closes the fill and removes its temporaries. The first call |
| +// releases the fill slot. Reserved bytes are released once the files they |
| +// cover are gone: if a removal fails, the bytes of the files that remain stay |
| +// reserved and a later call retries just those. The caller holds f.mu, or has |
| +// not shared f. |
| func (f *Fill) finish() { |
| - if f.done { |
| - return |
| + if !f.done { |
| + f.done = true |
| + if f.tmp != nil { |
| + f.tmp.Close() |
| + f.tmp = nil |
| + } |
| + if f.token != "" { |
| + f.left = []leftover{ |
| + {path: f.tmpName, size: f.res.identity}, |
| + {path: f.tempName("gz"), size: f.res.gzip}, |
| + } |
| + } |
| + f.s.releaseSlot(f.scope) |
| } |
| - f.done = true |
| - if f.tmp != nil { |
| - f.tmp.Close() |
| - f.tmp = nil |
| + var remaining int64 |
| + kept := f.left[:0] |
| + for _, l := range f.left { |
| + fi, err := os.Lstat(l.path) |
| + if errors.Is(err, os.ErrNotExist) { |
| + continue |
| + } |
| + if err == nil { |
| + l.size = fi.Size() // what the file really holds |
| + } |
| + if err := os.Remove(l.path); err == nil || errors.Is(err, os.ErrNotExist) { |
| + continue |
| + } |
| + kept = append(kept, l) |
| + remaining += l.size |
| } |
| - if f.token != "" { |
| - os.Remove(f.tmpName) |
| - os.Remove(f.tempName("gz")) |
| - } |
| - f.s.release(f.scope, f.res.total()) |
| + f.left = kept |
| + f.s.releaseReserved(f.held - remaining) |
| + f.held = remaining |
| } |
| -// release returns a reservation and its fill slot. |
| -func (s *Store) release(scope string, r int64) { |
| +// releaseSlot returns a fill slot. |
| +func (s *Store) releaseSlot(scope string) { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| - s.reserved -= r |
| s.fills-- |
| if s.fillsByApp[scope]--; s.fillsByApp[scope] <= 0 { |
| delete(s.fillsByApp, scope) |
| } |
| } |
| + |
| +// releaseReserved returns n reserved bytes. |
| +func (s *Store) releaseReserved(n int64) { |
| + if n == 0 { |
| + return |
| + } |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + s.reserved -= n |
| +} |
diff --git a/internal/assetcache/fill_test.go b/internal/assetcache/fill_test.go| index 6fd331524..fff17d350 100644 |
| --- a/internal/assetcache/fill_test.go |
| +++ b/internal/assetcache/fill_test.go |
| @@ -3,6 +3,7 @@ package assetcache |
| import ( |
| "errors" |
| "fmt" |
| + "os" |
| "sync" |
| "testing" |
| ) |
| @@ -326,7 +327,7 @@ func TestReserveAndAbortConcurrently(t *testing.T) { |
| go func(g int) { |
| defer wg.Done() |
| for i := 0; i < 50; i++ { |
| - f, err := s.Reserve(testKey(fmt.Sprintf("app%d", g%3), fmt.Sprintf("/%d/%d", g, i)), 64) |
| + f, err := s.Reserve(testKey([]string{"shop", "blog"}[g%2], fmt.Sprintf("/%d/%d", g, i)), 64) |
| if err != nil { |
| if !errors.Is(err, ErrBusy) { |
| t.Errorf("unexpected error: %v", err) |
| @@ -348,3 +349,58 @@ func TestReserveAndAbortConcurrently(t *testing.T) { |
| t.Fatalf("files left: %v", got) |
| } |
| } |
| + |
| +// TestAbortKeepsLeftoverBytesCountedAndRetries is not acceptance: it uses a |
| +// fake FS. When the temporaries cannot be removed (the directory is not |
| +// writable), the bytes still on disk stay counted, the fill slot is released, |
| +// and a second Abort removes the files and releases the bytes. |
| +func TestAbortKeepsLeftoverBytesCountedAndRetries(t *testing.T) { |
| + if os.Geteuid() == 0 { |
| + t.Skip("root ignores directory permissions") |
| + } |
| + s, _, dir := fillStore(t) |
| + f, err := s.Reserve(testKey("shop", "/a"), 100) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := f.Write([]byte("ten bytes!")); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(dir, 0o500); err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.Chmod(dir, 0o700) }) |
| + |
| + f.Abort() |
| + if got := s.Stats().Reserved; got != 10 { |
| + t.Fatalf("Reserved = %d after a failed cleanup, want the 10 bytes still on disk", got) |
| + } |
| + if got := listDir(t, dir); len(got) != 1 { |
| + t.Fatalf("files after a failed cleanup: %v, want the one body temporary", got) |
| + } |
| + s.mu.Lock() |
| + boxFills, appFills := s.fills, len(s.fillsByApp) |
| + s.mu.Unlock() |
| + if boxFills != 0 || appFills != 0 { |
| + t.Fatalf("slots after a failed cleanup: box %d, apps %d, want 0 and 0", boxFills, appFills) |
| + } |
| + f.Abort() // still failing: nothing changes, nothing is released twice |
| + if got := s.Stats().Reserved; got != 10 { |
| + t.Fatalf("Reserved = %d after a second failed cleanup, want 10", got) |
| + } |
| + |
| + if err := os.Chmod(dir, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.Abort() |
| + if got := s.Stats().Reserved; got != 0 { |
| + t.Fatalf("Reserved = %d after the retry, want 0", got) |
| + } |
| + if got := listDir(t, dir); len(got) != 0 { |
| + t.Fatalf("files after the retry: %v", got) |
| + } |
| + f.Abort() // and a further Abort is a no-op |
| + if got := s.Stats().Reserved; got != 0 { |
| + t.Fatalf("Reserved = %d after a third Abort", got) |
| + } |
| +} |