Skip to content

Serialize ImageStore state.json writes across instances with flock - #1032

Open
zs-andy wants to merge 1 commit into
apple:mainfrom
zs-andy:fix/imagestore-state-lock
Open

zs-andy wants to merge 1 commit into
apple:mainfrom
zs-andy:fix/imagestore-state-lock

Conversation

@zs-andy

@zs-andy zs-andy commented Nov 26, 2026

Copy link
Copy Markdown

Summary

ImageStore.ReferenceManager.create(description:) and delete(reference:) perform a read-modify-write cycle (load(), mutate dictionary, atomic save()) on state.json. Because ImageStore.lock and the ReferenceManager actor exist once per ImageStore instance, two instances operating on the same store directory (in the same process or across processes) can overwrite each other's updates, causing a subsequent cleanUpOrphanedBlobs() call to remove blobs belonging to dropped references.

Changes

  • Sources/Containerization/Image/ImageStore/ImageStore+ReferenceManager.swift:
    • Guard create(description:) and delete(reference:) with an exclusive flock(2) on <path>/state.json.lock (O_CREAT | O_RDWR | O_CLOEXEC, mode 0o600), retrying both open and flock on EINTR and releasing/closing on exit.
    • Import Darwin / Glibc / Musl consistently with SnapshotManager.swift.
  • Tests/ContainerizationTests/ImageTests/ImageStoreTests.swift:
    • Add testConcurrentCreateAcrossTwoStoreInstances
    • Add testConcurrentCreateAndDeleteAcrossTwoStoreInstances
    • Add testCleanUpOrphanedBlobsPreservesConcurrentlyLoadedImage

Scope note

This serializes state.json read-modify-write updates without changing ReferenceManager method signatures. As noted in #1029, the narrower cross-process window between completeIngestSession and recording the reference during pull/load vs. a concurrent cleanUpOrphanedBlobs() 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-nolint and .swift-format diff check on both modified files: clean.
  • Verified without the fix that the new concurrency tests fail deterministically due to lost updates.
  • 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

`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
zs-andy force-pushed the fix/imagestore-state-lock branch from 794995f to 8a92117 Compare November 26, 2026 01:58

@AustinKelsay AustinKelsay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zs-andy

zs-andy commented Nov 26, 2026

Copy link
Copy Markdown
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

No deployments
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.

[Bug]: Two ImageStore instances on one directory lose each other's references, then cleanUpOrphanedBlobs deletes the lost image's blobs

2 participants