Skip to content

snapshots/erofs: protect snapshot staging from cleanup - #13932

Merged
dmcgowan merged 1 commit into
containerd:mainfrom
cayoub-oai:codex/upstream-erofs-staging-transaction
Aug 10, 2026
Merged

dmcgowan merged 1 commit into
containerd:mainfrom
cayoub-oai:codex/upstream-erofs-staging-transaction

Conversation

@cayoub-oai

@cayoub-oai cayoub-oai commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Exclude in-flight EROFS new-* staging directories from orphan cleanup during
snapshot removal.

This prevents a concurrent Remove from deleting a directory that
createSnapshot is still preparing, without extending the metadata writer
transaction.

Problem

createSnapshot creates a snapshots/new-* directory before acquiring the
metadata writer transaction.

A concurrent Remove performs a global orphan scan of the snapshots directory.
Because the staging directory does not yet have a metadata ID, the scan can
classify it as an orphan and delete it. Snapshot creation then fails when it
tries to rename the missing staging directory:

failed to rename: rename .../snapshots/new-* .../snapshots/<id>: no such file or directory

This was observed downstream under concurrent image pulls and snapshot
removals.

Fix

Define a shared local prefix for snapshot staging directories and exclude
directories with that prefix from getCleanupDirectories.

Successful snapshot creation renames the staging directory to its allocated
metadata ID. Normal preparation failures continue to remove it through the
existing deferred cleanup.

A staging directory left by a daemon crash can be handled separately by a
future Cleanup implementation.

Fix EROFS snapshot creation failure caused by concurrent snapshot removal

Copilot AI lite review requested due to automatic review settings August 9, 2026 20:33
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Aug 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a race in the EROFS snapshotter where createSnapshot could create a snapshots/new-* staging directory before acquiring the metadata writer transaction, allowing a concurrent snapshot cleanup/remove to treat it as an orphan and delete it mid-flight. The change moves staging directory creation under the existing metadata writer transaction to ensure the on-disk staging directory and snapshot metadata lifecycle are serialized consistently.

Changes:

  • Move prepareDirectory(...) into the ms.WithTransaction(..., true, ...) writer transaction in createSnapshot.
  • Ensure the new-* staging directory is created immediately before storage.CreateSnapshot, preventing concurrent orphan cleanup from removing it.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@hsiangkao

Copy link
Copy Markdown
Member

hi Chris @cayoub-oai , I am not sure how to parse

A concurrent Remove can acquire the writer first, remove an unrelated
snapshot, and enumerate directories absent from the metadata ID map as orphans.
That enumeration includes the live new-* directory. After the transaction
commits, Remove deletes the directory.

how a unrelated Remove() removes a random temporary directory
td, err := os.MkdirTemp(snapshotDir, "new-").

Leaving prepareDirectory in the writer transaction will make the writer lock longer anyway, which is what we'd like to avoid.

@cayoub-oai

Copy link
Copy Markdown
Contributor Author

Sorry for the confusion - hopefully this helps clarify. The key detail is that Remove()(code) performs a global orphan scan, not cleanup limited to the snapshot being removed.

The race is:

  • createSnapshot creates snapshots/new-* before acquiring the writer transaction (code).
  • An unrelated Remove acquires the writer first and calls getCleanupDirectories (code).
  • That function scans all snapshot directories and selects anything absent from the metadata ID map (code). The in-flight new-* directory has no metadata yet, so it is selected and deleted (code).
  • createSnapshot then tries to rename the deleted directory and gets ENOENT (code).

You are right that this change lengthens the writer transaction; that is the trade-off used here to close the untracked-directory window.

@hsiangkao

hsiangkao commented Aug 10, 2026 •

Copy link
Copy Markdown
Member
  • That function scans all snapshot directories and selects anything absent from the metadata ID map (code). The in-flight new-* directory has no metadata yet, so it is selected and deleted (code).

I guess we could just exclude the directory names starting with new- in getCleanupDirectories() directly? since they should be renamed as a follow-up.

@hsiangkao

Copy link
Copy Markdown
Member

The worst case is that we could cleanup all directories starting with new- in the Cleanup() method if any (it should be due to daemon crashes for example)

A new-* directory can exist before createSnapshot acquires the metadata
writer transaction. A concurrent Remove performs a global orphan scan and
can delete the staging directory because it is not in the ID map yet.

Exclude new-* staging directories from orphan cleanup and use a shared
local constant for directory creation and filtering.

Signed-off-by: Chris Ayoub <[email protected]>
Copilot AI review requested due to automatic review settings August 10, 2026 03:44
@cayoub-oai
cayoub-oai force-pushed the codex/upstream-erofs-staging-transaction branch from 9ccc43c to e940b5a Compare August 10, 2026 03:44
@kubernetes-prow kubernetes-prow Bot added size/M and removed size/S labels Aug 10, 2026
@cayoub-oai

Copy link
Copy Markdown
Contributor Author

@hsiangkao updated - let me know what you think

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

plugins/snapshots/erofs/erofs.go:901

  • Skipping all directories with the temp prefix here prevents orphan cleanup from ever reclaiming crash-leftover staging directories, and it diverges from the PR description (“move prepareDirectory into the writer transaction” / “does not special-case new-*”). Since erofs.createSnapshot still creates the temp dir before acquiring the writer transaction (plugins/snapshots/erofs/erofs.go:618), this check is acting as a workaround but changes cleanup semantics.

Consider aligning with the overlay snapshotter by moving prepareDirectory (and snapshotDir computation) inside the existing ms.WithTransaction(..., true, ...) in createSnapshot (overlay does this at plugins/snapshots/overlay/overlay.go:454-462). If you do that, this special-casing can be removed and orphan cleanup can continue to reclaim abandoned temp dirs.

		// A new-* directory may belong to a concurrent snapshot creation. It is
		// renamed to its metadata ID after the writer transaction is acquired, so
		// Remove must not treat it as an orphan in the meantime.
		if strings.HasPrefix(d, snapshotTempDirPrefix) {
			continue

plugins/snapshots/erofs/erofs_linux_test.go:128

  • This test codifies the behavior that new-* staging directories are never considered for cleanup. That implies a staging directory left behind by a crashed process will not be reclaimed by subsequent orphan cleanup (since getCleanupDirectories is only used by Remove in this snapshotter).

If the intended fix is to avoid cleanup races without leaking temp dirs, prefer moving temp-dir creation into the writer transaction (like the overlay snapshotter) and update this test to assert that an unowned temp dir is returned for cleanup (i.e., it’s treated like any other orphan).

	_, err = os.MkdirTemp(snapshotDir, snapshotTempDirPrefix)
	require.NoError(t, err)

@hsiangkao hsiangkao added cherry-pick/2.1.x Change to be cherry picked to release/2.1 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch cherry-pick/2.3.x Change to be cherry picked to release/2.3 labels Aug 10, 2026

@djdongjin djdongjin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@github-project-automation github-project-automation Bot moved this from Needs Triage to Review In Progress in Pull Request Review Aug 10, 2026
@dmcgowan
dmcgowan added this pull request to the merge queue Aug 10, 2026
Merged via the queue into containerd:main with commit 58e6cee Aug 10, 2026
89 of 90 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Aug 10, 2026
@hsiangkao

Copy link
Copy Markdown
Member

/cherry-pick release/2.3
/cherry-pick release/2.3

@hsiangkao

Copy link
Copy Markdown
Member

/cherry-pick release/2.2

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@hsiangkao: #13932 failed to apply on top of branch "release/2.3":

Applying: snapshots/erofs: protect snapshot staging from cleanup
Using index info to reconstruct a base tree...
M	plugins/snapshots/erofs/erofs.go
M	plugins/snapshots/erofs/erofs_linux_test.go
Falling back to patching base and 3-way merge...
Auto-merging plugins/snapshots/erofs/erofs.go
CONFLICT (content): Merge conflict in plugins/snapshots/erofs/erofs.go
Auto-merging plugins/snapshots/erofs/erofs_linux_test.go
CONFLICT (content): Merge conflict in plugins/snapshots/erofs/erofs_linux_test.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 snapshots/erofs: protect snapshot staging from cleanup

Details

In response to this:

/cherry-pick release/2.3
/cherry-pick release/2.3

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@hsiangkao: #13932 failed to apply on top of branch "release/2.2":

Applying: snapshots/erofs: protect snapshot staging from cleanup
Using index info to reconstruct a base tree...
M	plugins/snapshots/erofs/erofs.go
M	plugins/snapshots/erofs/erofs_linux_test.go
Falling back to patching base and 3-way merge...
Auto-merging plugins/snapshots/erofs/erofs.go
CONFLICT (content): Merge conflict in plugins/snapshots/erofs/erofs.go
Auto-merging plugins/snapshots/erofs/erofs_linux_test.go
CONFLICT (content): Merge conflict in plugins/snapshots/erofs/erofs_linux_test.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 snapshots/erofs: protect snapshot staging from cleanup

Details

In response to this:

/cherry-pick release/2.2

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/snapshotters Snapshotters cherry-pick/2.1.x Change to be cherry picked to release/2.1 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch cherry-pick/2.3.x Change to be cherry picked to release/2.3 size/M

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants