Skip to content

fix(storage): count real orphan blob deletions, own orphan cursors and surface lost blob locks - #422

Merged
M0Rf30 merged 1 commit into
mainfrom
fix/blob-refcount
Nov 4, 2026
Merged

M0Rf30 merged 1 commit into
mainfrom
fix/blob-refcount

Conversation

@M0Rf30

@M0Rf30 M0Rf30 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Fixes the blob refcount / blob lock findings of the code audit.

Fixed

  1. Lost blob lock reported as success. WithBlobLocks logged a failed tx.Commit and returned nil, so a holder whose lock session died (idle_in_transaction_session_timeout, failover, pg_terminate_backend) believed it had run exclusively. It now returns database.ErrBlobLockLost (wrapping the commit error). In addition every lock transaction (WithBlobLocks, DeleteBlobIfUnreferenced, the batched delete, FlipBlobPlacement, which runs verify against the object store) executes set_config('idle_in_transaction_session_timeout','0',true) with its first lock statement, so a server-wide timeout can no longer kill an idle lock transaction in the first place. All callers go through storage.WithBlobLock(s) / CommitBlobs and already return the error.
  2. Orphan count included blobs that were not removed. The collector did removed++ whenever SafeDeleteBlob returned nil, even when DeleteBlobIfUnreferenced returned false (a row references the blob again). The bool now flows through an unexported safeDeleteBlob and only real deletions are counted. The exported SafeDeleteBlob keeps its error-only signature: ~60 call sites ignore the outcome and none needs it.
  3. Partial batch counts. removed += n is now done before the batch error is looked at, and orphanDeleteChunk is tied to database.BlobBatchLockChunk so one DeleteBlobsIfUnreferenced call is one transaction (commits all or nothing); this also guarantees the per-blob fallback cannot count a blob twice. Today n is 0 on error, so this is defensive rather than a visible change.
  4. Shared global orphanCursor. Replaced by storage.OrphanCursor, owned by each caller: CleanupWorker, TrashPurgeWorker, RepositoriesHandler and TrashHandler. A cursor is mutex-protected, held for a whole window (concurrent passes on one cursor take consecutive windows) and advanced only when the window was worked through (a pass cut short by shutdown redoes the window instead of skipping it).
  5. lockSlots nesting rule. Documented on core.lockSlots, acquireLockSlot, WithBlobLocks, FlipBlobPlacement, SafeDeleteBlob and storage.WithBlobLock. No runtime assertion: a marker in the context would break production code that nests today (see below).

Skipped

  1. ReclaimMetadataBlob vs a same-sha blobs row. Not a bug. blobRefCount counts FK references when the row exists, and BlobDeletionSweeper.bytesNeeded re-checks under the blob lock: for scope All it keeps the bytes and drops the queue entry if BlobReferenceCount > 0 or a blobs row exists (GetBlobPlacement found). A dangling row or lost bytes is impossible; an unreferenced same-sha row is later removed by the orphan collector, which re-queues the bytes. Pinned by a new integration test (TestReclaimMetadataBlobLeavesAnUnreferencedBlobsRowIntact).

Tests

  • storage: only real removals counted (queue path and per-blob fallback), cursors are independent and continue their own sweep, cursor stays put when the pass is cancelled.
  • database (integration): killing the lock session inside fn yields ErrBlobLockLost; a lock held 3x longer than a 200 ms idle_in_transaction_session_timeout survives (the test fails with the set_config removed, with a control transaction proving the timeout is in effect).
  • tests (integration): orphan sweep returns exactly the removed blobs; item 5 above.

Notes / not changed

  • A false failure is possible: if the lock transaction dies after fn committed, the caller sees an error although its rows exist (e.g. gomod handleUpload releases its quota reservation; the periodic quota reconcile corrects it). SafeDeleteBlob cleanup after such a failure is refcount-guarded, so no referenced bytes are lost.
  • Not fixed, out of scope: internal/format/docker/attach.go AttachReferrer runs storage.WithBlobLock and inside it attachLocked calls storage.CommitBlob (nested WithBlobLocks) and SafeDeleteBlob. That violates the no-nesting rule and can deadlock once MaxOpenConns/2 concurrent attaches fill the slots.

…d surface lost blob locks

- WithBlobLocks returns ErrBlobLockLost when the lock transaction cannot be
  committed (its session died while fn ran) instead of logging and reporting
  success. The lock transactions (WithBlobLocks, DeleteBlobIfUnreferenced, the
  batched delete, FlipBlobPlacement) switch off idle_in_transaction_session_timeout
  for themselves so a server setting cannot drop the lock.
- The orphan collector counts a blob only when it was handed to deletion: one a
  row references again is kept and no longer counted. What a batch committed
  before an error is kept in the count; the orphan chunk now equals the
  database's lock chunk, so one batch call is one transaction.
- The process-wide orphan cursor is replaced by an OrphanCursor owned by each
  caller (cleanup worker, trash purge worker, repository and trash handlers), so
  one caller's passes no longer move another's position past unexamined blobs.
  A window cut short by shutdown is redone instead of skipped.
- Document the no-nesting rule for blob lock slots on WithBlobLocks,
  FlipBlobPlacement, SafeDeleteBlob and core.lockSlots.
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Coverage

Coverage vs main
Go, unit + integration 83.2% ±0.0
Go, unit tests only 69.7% ±0.0
Go, integration tests only 54.0% ±0.0
Web, lines 90.4% ±0.0
Web, functions 84.8% ±0.0
Go by area
Area Coverage vs main
cmd/docgen 80.3% ±0.0
cmd/genformats 90.8% ±0.0
cmd/oa 0.0% ±0.0
cmd/resetpw 0.0% ±0.0
cmd/server 70.5% ±0.0
internal/ai 85.6% ±0.0
internal/api 76.5% ±0.0
internal/apkkeys 77.1% ±0.0
internal/artifactpath 100.0% ±0.0
internal/attest 85.6% ±0.0
internal/audit 97.6% ±0.0
internal/auth 91.0% ±0.0
internal/backup 70.0% ±0.0
internal/bgtask 96.8% ±0.0
internal/branding 93.5% ±0.0
internal/clientsetup 93.6% ±0.0
internal/config 96.4% ±0.0
internal/cron 90.2% ±0.0
internal/crypto 90.7% ±0.0
internal/curation 98.9% ±0.0
internal/database 77.5% ±0.0
internal/declarative 83.4% ±0.0
internal/dtrack 85.3% ±0.0
internal/exploit 93.6% ±0.0
internal/federation 80.9% -0.1
internal/format 80.8% ±0.0
internal/formats 87.9% ±0.0
internal/gpgkeys 87.1% +0.3
internal/group 89.6% ±0.0
internal/httpcache 100.0% ±0.0
internal/httpcompress 93.3% ±0.0
internal/httpx 85.7% ±0.0
internal/importmeta 93.3% ±0.0
internal/iocopy 100.0% ±0.0
internal/itempath 98.3% ±0.0
internal/lazyre 100.0% ±0.0
internal/license 99.4% ±0.0
internal/licensemeta 87.6% ±0.0
internal/licensepolicy 92.6% ±0.0
internal/limitio 92.9% ±0.0
internal/mcp 89.3% ±0.0
internal/metrics 96.2% ±0.0
internal/middleware 94.0% ±0.0
internal/netutil 93.1% ±0.0
internal/nixkeys 79.6% ±0.0
internal/oacli 84.5% ±0.0
internal/ocimanifest 100.0% ±0.0
internal/ocipath 100.0% ±0.0
internal/osv 88.5% ±0.0
internal/passwd 93.3% ±0.0
internal/pathseg 100.0% ±0.0
internal/pglock 77.8% ±0.0
internal/pkgdeps 88.1% ±0.0
internal/presign 99.2% ±0.0
internal/promote 84.6% ±0.0
internal/proxy 86.6% ±0.0
internal/publicurl 96.8% ±0.0
internal/pypisimple 97.9% ±0.0
internal/quarantine 100.0% ±0.0
internal/quota 87.8% ±0.0
internal/recovery 85.9% +0.6
internal/registry 100.0% ±0.0
internal/replication 78.3% +0.1
internal/repodispatch 84.8% ±0.0
internal/repotype 97.0% ±0.0
internal/sbom 90.0% ±0.0
internal/scanexport 96.4% ±0.0
internal/search 90.7% ±0.0
internal/secrets 90.5% ±0.0
internal/storage 84.8% +0.1
internal/telemetry 92.2% ±0.0
internal/tiering 83.1% ±0.0
internal/trash 96.7% ±0.0
internal/urlparams 100.0% ±0.0
internal/vuln 89.5% +0.1
internal/webhook 83.1% ±0.0
internal/worker 82.4% ±0.0
internal/workq 82.1% +3.4
pkg/ansible 90.2% ±0.0
pkg/apk 96.0% ±0.0
pkg/cargo 95.6% ±0.0
pkg/compactindex 97.6% ±0.0
pkg/composer 94.5% ±0.0
pkg/conan 99.2% ±0.0
pkg/conda 94.8% ±0.0
pkg/deb 76.5% ±0.0
pkg/delta 71.4% ±0.0
pkg/gemversion 100.0% ±0.0
pkg/gpg 86.7% ±0.0
pkg/helm 96.9% ±0.0
pkg/logger 93.8% ±0.0
pkg/maven 96.7% ±0.0
pkg/nix 99.1% ±0.0
pkg/npm 88.0% ±0.0
pkg/nuget 97.6% ±0.0
pkg/pacman 90.4% ±0.0
pkg/pyversion 100.0% ±0.0
pkg/rpm 91.8% ±0.0
pkg/rpmsign 90.5% ±0.0
pkg/rubymarshal 86.7% ±0.0
tests/integration 78.8% ±0.0
web 93.7% ±0.0
Web by area (lines)
Area Coverage vs main
src 24.2% ±0.0
src/api 91.6% ±0.0
src/components 89.3% ±0.0
src/composables 92.4% ±0.0
src/i18n 98.4% ±0.0
src/lib 99.2% ±0.0
src/pages 89.3% ±0.0
src/pwa 97.6% ±0.0
src/router 99.3% ±0.0
src/stores 99.0% ±0.0
src/telemetry 90.9% ±0.0

Full report (HTML, merged profile) in the CI run artifacts

@M0Rf30
M0Rf30 merged commit 6334ff5 into main Nov 4, 2026
25 checks passed
@M0Rf30
M0Rf30 deleted the fix/blob-refcount branch November 4, 2026 19:52
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