From 5a6a980dade5fa78ae46dccc8b971950f4d21378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Mon, 13 Jan 2025 14:45:59 +0100 Subject: [PATCH] daemon/export: Stop when context is canceled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close archive when context is done - this makes the cancellation actually stop the export instead of continuing it regardless if the client still expects the data. Signed-off-by: Paweł Gronowski --- daemon/export.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/daemon/export.go b/daemon/export.go index 00903496b9..d01057cd26 100644 --- a/daemon/export.go +++ b/daemon/export.go @@ -71,11 +71,17 @@ func (daemon *Daemon) containerExport(ctx context.Context, ctr *container.Contai return err } - if err := ctx.Err(); err != nil { - return err - } + ctx, cancel := context.WithCancel(ctx) + defer cancel() + context.AfterFunc(ctx, func() { + _ = archv.Close() + }) + // Stream the entire contents of the container (basically a volatile snapshot) if _, err := io.Copy(out, archv); err != nil { + if err := ctx.Err(); err != nil { + return errdefs.Cancelled(err) + } return err }