Repository navigation
Conversation
`ImageStore.ReferenceManager.create(description:)` and `delete(reference:)` read, mutate, and atomically rewrite `state.json`. Because `ImageStore.lock` and the `ReferenceManager` actor exist per instance, two `ImageStore` instances sharing a directory (in the same process or across processes) can overwrite each other's updates, and a subsequent `cleanUpOrphanedBlobs()` can delete blobs for the dropped references. Guard both read-modify-write paths with an exclusive `flock(2)` on `<path>/state.json.lock` (retrying `open` and `flock` on `EINTR`), matching the Darwin/Glibc/Musl POSIX locking pattern used in `SnapshotManager`. Fixes apple#1029
zs-andy
force-pushed
the
fix/imagestore-state-lock
branch
from
November 26, 2026 01:58
794995f to
8a92117
Compare
AustinKelsay
left a comment
There was a problem hiding this comment.
Thanks for picking this up. I filed #1029 and ran its reproduction against this branch (8a92117) on macOS arm64:
| Reproduction | 0.57.0 | This PR |
|---|---|---|
| A. Two instances in one process, 200 records each | 137 and 64 kept | 200 and 200 |
| B. Two processes, 200 records each | 115 and 89 kept | 200 and 200 |
| C. Pull alpine while another process tags 3000 times, then cleanup | Record lost in 4 of 5 rounds, its blobs deleted | Kept in 5 of 5, 0 blobs deleted |
swift test -Xswiftc -warnings-as-errors --filter ImageStoreTests passes 10 of 10, in four runs.
This is the fix I proposed in the issue. The EINTR handling and the create/delete and cleanup tests go further than mine did. It looks good to me as the reporter.
3 tasks done
Author
|
Thanks for verifying all three repros (including the multi-process and pull/cleanup runs) on this branch, and for the detailed report in #1029. |
This branch has not been deployed
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.
Summary
ImageStore.ReferenceManager.create(description:)anddelete(reference:)perform a read-modify-write cycle (load(), mutate dictionary, atomicsave()) onstate.json. BecauseImageStore.lockand theReferenceManageractor exist once perImageStoreinstance, two instances operating on the same store directory (in the same process or across processes) can overwrite each other's updates, causing a subsequentcleanUpOrphanedBlobs()call to remove blobs belonging to dropped references.Changes
Sources/Containerization/Image/ImageStore/ImageStore+ReferenceManager.swift:create(description:)anddelete(reference:)with an exclusiveflock(2)on<path>/state.json.lock(O_CREAT | O_RDWR | O_CLOEXEC, mode0o600), retrying bothopenandflockonEINTRand releasing/closing on exit.Darwin/Glibc/Muslconsistently withSnapshotManager.swift.Tests/ContainerizationTests/ImageTests/ImageStoreTests.swift:testConcurrentCreateAcrossTwoStoreInstancestestConcurrentCreateAndDeleteAcrossTwoStoreInstancestestCleanUpOrphanedBlobsPreservesConcurrentlyLoadedImageScope note
This serializes
state.jsonread-modify-write updates without changingReferenceManagermethod signatures. As noted in #1029, the narrower cross-process window betweencompleteIngestSessionand recording the reference duringpull/loadvs. a concurrentcleanUpOrphanedBlobs()is not covered by this lock alone.Testing
Tested locally on macOS arm64 (Swift 6.4; Linux build not run locally):
swift format lint --recursive --strict --configuration .swift-format-nolintand.swift-formatdiff check on both modified files: clean.swift test -Xswiftc -warnings-as-errors --filter ImageStoreTests: 10/10 passed across 5 consecutive runs.swift test -Xswiftc -warnings-as-errors --filter "ImageDigestPoisoningTests|ImageStorePlatformsTests|ImageStoreImagePullTests|SnapshotManagerTests": 30/30 passed across 4 suites.Fixes #1029