From f14f444a4a6551dc37ae4cf83e15c8527b72c0a0 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 15 Jun 2026 18:50:19 -0300 Subject: [PATCH] test(differ): poll for async loop detach in checkNoLoopDevice checkNoLoopDevice did a single immediate FindByBackingFile check after the temp mount cleanup returned. The kernel releases loop devices asynchronously after LOOP_CLR_FD / autoclear, so the backing file can still resolve to a device for a short window, causing flaky failures: loop device /dev/loop1 still attached to .../layer.erofs (seen in TestWithStackedErofsTempMount and TestWithActiveSnapshotMountOverlay) Poll with a 5s deadline, matching the existing mountutils.verifyNoLoopDevice and loop.verifyDetached helpers, which already handle the same race. Co-Authored-By: Claude Opus 4.8 (1M context) --- internal/differ/compare_linux_test.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/internal/differ/compare_linux_test.go b/internal/differ/compare_linux_test.go index 04fbbfb..ca3d4eb 100644 --- a/internal/differ/compare_linux_test.go +++ b/internal/differ/compare_linux_test.go @@ -15,6 +15,7 @@ import ( "strings" "sync" "testing" + "time" "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/core/diff" @@ -897,13 +898,25 @@ func populateExt4(t *testing.T, img string, fill func(root string)) { // the given backing files after the with*Mount helpers returned. func checkNoLoopDevice(t *testing.T, backingFiles ...string) { t.Helper() + // The kernel releases loop devices asynchronously after LOOP_CLR_FD / + // autoclear, so a backing file may still resolve to a device for a short + // window after cleanup returns. Poll with a deadline, matching + // mountutils.verifyNoLoopDevice and loop.verifyDetached. for _, f := range backingFiles { - dev, err := loop.FindByBackingFile(f) - if err != nil { - t.Fatalf("failed to look up loop device for %s: %v", f, err) - } - if dev != nil { - t.Errorf("loop device %s still attached to %s", dev.Path, f) + deadline := time.Now().Add(5 * time.Second) + for { + dev, err := loop.FindByBackingFile(f) + if err != nil { + t.Fatalf("failed to look up loop device for %s: %v", f, err) + } + if dev == nil { + break + } + if time.Now().After(deadline) { + t.Errorf("loop device %s still attached to %s", dev.Path, f) + break + } + time.Sleep(10 * time.Millisecond) } } }