From 3294c68b993fde23a3e118a9bfc5be826350f4af Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 14 Aug 2024 13:00:56 +0300 Subject: [PATCH] uploadprovider: allow closing used sources Helps detecting when uploadprovider has finished with the source. This avoids deadlock, should the same reader be shared by multiple uploads that sync with each other. Signed-off-by: Tonis Tiigi --- frontend/dockerfile/dockerfile_test.go | 4 ++-- session/upload/uploadprovider/provider.go | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/frontend/dockerfile/dockerfile_test.go b/frontend/dockerfile/dockerfile_test.go index 3530b7a19131..bd2e3aa459bc 100644 --- a/frontend/dockerfile/dockerfile_test.go +++ b/frontend/dockerfile/dockerfile_test.go @@ -5256,7 +5256,7 @@ COPY foo / defer c.Close() up := uploadprovider.New() - url := up.Add(buf) + url := up.Add(io.NopCloser(buf)) _, err = f.Solve(sb.Context(), c, client.SolveOpt{ FrontendAttrs: map[string]string{ @@ -5301,7 +5301,7 @@ COPY foo bar defer c.Close() up := uploadprovider.New() - url := up.Add(buf) + url := up.Add(io.NopCloser(buf)) // repeat with changed default args should match the old cache destDir := t.TempDir() diff --git a/session/upload/uploadprovider/provider.go b/session/upload/uploadprovider/provider.go index 010959f43dcc..2af434d8063c 100644 --- a/session/upload/uploadprovider/provider.go +++ b/session/upload/uploadprovider/provider.go @@ -13,15 +13,15 @@ import ( ) func New() *Uploader { - return &Uploader{m: map[string]io.Reader{}} + return &Uploader{m: map[string]io.ReadCloser{}} } type Uploader struct { mu sync.Mutex - m map[string]io.Reader + m map[string]io.ReadCloser } -func (hp *Uploader) Add(r io.Reader) string { +func (hp *Uploader) Add(r io.ReadCloser) string { id := identity.NewID() hp.m[id] = r return "http://buildkit-session/" + id @@ -51,6 +51,11 @@ func (hp *Uploader) Pull(stream upload.Upload_PullServer) error { hp.mu.Unlock() _, err := io.Copy(&writer{stream}, r) + + if err1 := r.Close(); err == nil { + err = err1 + } + return err }