Skip to content

fix(docker): never nest blob locks inside the referrer slot lock - #423

Merged
M0Rf30 merged 1 commit into
mainfrom
fix/docker-attach-lock-nesting
Nov 4, 2026
Merged

M0Rf30 merged 1 commit into
mainfrom
fix/docker-attach-lock-nesting

Conversation

@M0Rf30

@M0Rf30 M0Rf30 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

AttachReferrer held a per-slot lock (storage.WithBlobLock) while attachLocked called storage.CommitBlob (layer, config, manifest) and storage.SafeDeleteBlob (orphan cleanup, replaced manifest). Each of those takes its own blob lock, and every lock holder pins one of the bounded lock slots (max(1, MaxOpenConns/2), core.lockSlots). Once as many concurrent attaches as slots held the outer lock, each waited for an inner slot only another waiter could free: a deadlock. With 2 or 3 pooled connections (one slot) a single attach could not finish. discardOrphan runs on context.WithoutCancel, so that waiter never gave up either.

Change

No lock-taking call runs inside a locked section any more:

  • layer and config blobs are committed before the slot lock, each under its own blob lock;
  • the referrer manifest is stored and the slot decided under the slot lock and the manifest blob's lock, taken together on one connection (new storage.CommitBlobsUnder: CommitBlobs plus extra lock keys, sorted with the blob locks, so no deadlock with other lockers). The per-slot single writer and the "blob not deleted between check and row insert" guarantees both still hold;
  • the replaced referrer's rows are removed in that same section (deleteManifestRows); its content blob is reclaimed afterwards by SafeDeleteBlob under its own lock, after the rows are committed (the standard GC protocol);
  • an identical referrer already attached is still answered with one query and no lock.

hardDeleteManifest is now deleteManifestRows + reclaimManifestBlob; storeManifest is commitManifest + insertManifest. Other callers are unchanged. CommitBlobs documents that its commit callback must not take a blob lock.

Tests

  • TestAttachReferrerNeverNestsBlobLocks (unit): a fake whose blob locks refuse nesting; also checks the referrer row is written under exactly the slot lock and the manifest's, and the replaced row is deleted in the same section.
  • TestAttachReferrerConcurrentAttachesDoNotDeadlockTheLockSlots (integration): 12 concurrent attaches (4 slots x 3 racing writers) on a 4-connection pool (2 lock slots) while the orphan collector runs back to back with minAge 0; asserts exactly one referrer per slot and intact manifest blobs. Fails after 30 s on a deadlock.
  • TestCommitBlobsUnder* (storage).

Both new attach tests fail on main (unit: nested WithBlobLocks/DeleteBlobIfUnreferenced reported; integration: "blob lock slots are deadlocked" after 30 s, and hung until the go test timeout before the fail-fast guard) and pass here (integration test ran 20x with -race).

AttachReferrer held a per-slot lock (storage.WithBlobLock) while
attachLocked called storage.CommitBlob (layer, config, manifest) and
SafeDeleteBlob (orphan cleanup, replaced manifest). Each of those takes
its own blob lock, and every lock holder pins one of the bounded lock
slots (max(1, MaxOpenConns/2)). With as many concurrent attaches as
slots, every slot was held by an outer lock waiting for an inner one: a
deadlock, and with 2 or 3 pooled connections (one slot) even a single
attach could not finish. discardOrphan runs on a context without
cancellation, so the waiters never gave up.

Restructure so no lock-taking call runs inside a locked section:

- the layer and config blobs are committed before the slot is locked,
  each under its own blob lock;
- the referrer manifest is stored and the slot decided under the slot
  lock and the manifest blob's lock taken together on one connection
  (new storage.CommitBlobsUnder, which adds extra lock keys to the blob
  locks CommitBlobs takes), so the per-slot single-writer guarantee and
  the "no delete between check and insert" guarantee both hold;
- the replaced referrer's rows are removed in that same section
  (deleteManifestRows) and its content blob is reclaimed afterwards with
  SafeDeleteBlob, under its own lock, after the rows are committed;
- an identical referrer already attached is still answered with one
  query and no lock.

CommitBlobs documents that its commit callback must not take a blob
lock either.

Tests: a unit test fails when any blob lock is taken while another is
held and checks which locks guard the referrer row writes; an
integration test runs 12 concurrent attaches (3 racing writers per slot)
on a 4-connection pool (2 lock slots) while the orphan collector runs
back to back, and fails after 30s on a deadlock (it hung before this
change). CommitBlobsUnder has its own storage tests.
@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.5% +0.5
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.6% +0.1
internal/declarative 83.4% ±0.0
internal/dtrack 85.3% ±0.0
internal/exploit 93.6% ±0.0
internal/federation 81.1% +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.4% +0.1
internal/registry 100.0% ±0.0
internal/replication 78.2% ±0.0
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.7% ±0.0
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.5% +0.1
internal/workq 78.6% ±0.0
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 8533804 into main Nov 4, 2026
25 checks passed
@M0Rf30
M0Rf30 deleted the fix/docker-attach-lock-nesting branch November 4, 2026 19:52
@M0Rf30 M0Rf30 mentioned this pull request Nov 4, 2026
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