layer: Clean up init layer if initialization fails

Add cleanup for the init layer directory if any operation fails after
driver.CreateReadWrite() succeeds in initMount(). Previously, failures
in driver.Get(), initFunc(), or driver.Put() would leave an orphaned
overlay2 directory.

Related to moby/moby#45939

Signed-off-by: Jan Scheffler <jan.scheffler@qodev.ai>
This commit is contained in:
Jan Scheffler
2025-12-15 11:06:56 +00:00
parent 0c01da8ccc
commit 3fdde529e7

View File

@@ -660,6 +660,17 @@ func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc Mou
if err := ls.driver.CreateReadWrite(initID, parent, createOpts); err != nil {
return "", err
}
// Clean up init layer if any subsequent operation fails
successful := false
defer func() {
if !successful {
if err := ls.driver.Remove(initID); err != nil {
log.G(context.TODO()).WithFields(log.Fields{"init-id": initID, "error": err}).Error("Failed to clean up init layer after error")
}
}
}()
p, err := ls.driver.Get(initID, "")
if err != nil {
return "", err
@@ -674,6 +685,7 @@ func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc Mou
return "", err
}
successful = true
return initID, nil
}