From b51f8ea20077fb36c1a4646a9f28d0a24d8e5957 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 13 Dec 2022 09:48:21 -0500 Subject: [PATCH] proxy: Ensure images are closed when proxy is shutting down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a complementary fix for https://github.com/coreos/rpm-ostree/issues/4213 Basically in the case of `oci-archive` we have a temporary directory that needs cleanup. Signed-off-by: Colin Walters Co-authored-by: Miloslav Trmač Signed-off-by: Colin Walters --- cmd/skopeo/proxy.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/skopeo/proxy.go b/cmd/skopeo/proxy.go index ee46f47f..d3423638 100644 --- a/cmd/skopeo/proxy.go +++ b/cmd/skopeo/proxy.go @@ -75,12 +75,14 @@ import ( "github.com/containers/image/v5/manifest" ocilayout "github.com/containers/image/v5/oci/layout" "github.com/containers/image/v5/pkg/blobinfocache" + "github.com/containers/image/v5/transports" "github.com/containers/image/v5/transports/alltransports" "github.com/containers/image/v5/types" dockerdistributionerrcode "github.com/docker/distribution/registry/api/errcode" dockerdistributionapi "github.com/docker/distribution/registry/api/v2" "github.com/opencontainers/go-digest" imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -701,6 +703,17 @@ func (h *proxyHandler) FinishPipe(args []interface{}) (replyBuf, error) { return ret, err } +// close releases all resources associated with this proxy backend +func (h *proxyHandler) close() { + for _, image := range h.images { + err := image.src.Close() + if err != nil { + // This shouldn't be fatal + logrus.Warnf("Failed to close image %s: %v", transports.ImageName(image.cachedimg.Reference()), err) + } + } +} + // send writes a reply buffer to the socket func (buf replyBuf) send(conn *net.UnixConn, err error) error { replyToSerialize := reply{ @@ -816,6 +829,7 @@ func (opts *proxyOptions) run(args []string, stdout io.Writer) error { images: make(map[uint32]*openImage), activePipes: make(map[uint32]*activePipe), } + defer handler.close() // Convert the socket FD passed by client into a net.FileConn fd := os.NewFile(uintptr(opts.sockFd), "sock")