Browse code

daemon/export: Stop when context is canceled

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 <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/01/13 22:45:59
Showing 1 changed files
... ...
@@ -71,11 +71,17 @@ func (daemon *Daemon) containerExport(ctx context.Context, ctr *container.Contai
71 71
 		return err
72 72
 	}
73 73
 
74
-	if err := ctx.Err(); err != nil {
75
-		return err
76
-	}
74
+	ctx, cancel := context.WithCancel(ctx)
75
+	defer cancel()
76
+	context.AfterFunc(ctx, func() {
77
+		_ = archv.Close()
78
+	})
79
+
77 80
 	// Stream the entire contents of the container (basically a volatile snapshot)
78 81
 	if _, err := io.Copy(out, archv); err != nil {
82
+		if err := ctx.Err(); err != nil {
83
+			return errdefs.Cancelled(err)
84
+		}
79 85
 		return err
80 86
 	}
81 87