diff --git a/daemon/graphdriver/windows/windows.go b/daemon/graphdriver/windows/windows.go index b4fec2eb9d..3e5267dd24 100644 --- a/daemon/graphdriver/windows/windows.go +++ b/daemon/graphdriver/windows/windows.go @@ -779,7 +779,7 @@ func writeLayerReexec() { } // writeLayer writes a layer from a tar file. -func writeLayer(layerData io.Reader, home string, id string, parentLayerPaths ...string) (int64, error) { +func writeLayer(layerData io.Reader, home string, id string, parentLayerPaths ...string) (size int64, retErr error) { err := winio.EnableProcessPrivileges([]string{winio.SeBackupPrivilege, winio.SeRestorePrivilege}) if err != nil { return 0, err @@ -804,17 +804,17 @@ func writeLayer(layerData io.Reader, home string, id string, parentLayerPaths .. return 0, err } - size, err := writeLayerFromTar(layerData, w, filepath.Join(home, id)) - if err != nil { - return 0, err - } + defer func() { + if err := w.Close(); err != nil { + // This error should not be discarded as a failure here + // could result in an invalid layer on disk + if retErr == nil { + retErr = err + } + } + }() - err = w.Close() - if err != nil { - return 0, err - } - - return size, nil + return writeLayerFromTar(layerData, w, filepath.Join(home, id)) } // resolveID computes the layerID information based on the given id.