Repository navigation
fix(docker): never nest blob locks inside the referrer slot lock - #423
Merged
Merged
Conversation
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.
Coverage
Go by area
Web by area (lines)
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AttachReferrerheld a per-slot lock (storage.WithBlobLock) whileattachLockedcalledstorage.CommitBlob(layer, config, manifest) andstorage.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.discardOrphanruns oncontext.WithoutCancel, so that waiter never gave up either.Change
No lock-taking call runs inside a locked section any more:
storage.CommitBlobsUnder:CommitBlobsplus 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;deleteManifestRows); its content blob is reclaimed afterwards bySafeDeleteBlobunder its own lock, after the rows are committed (the standard GC protocol);hardDeleteManifestis nowdeleteManifestRows+reclaimManifestBlob;storeManifestiscommitManifest+insertManifest. Other callers are unchanged.CommitBlobsdocuments 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 withminAge 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: nestedWithBlobLocks/DeleteBlobIfUnreferencedreported; 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).