From be96014740fb37d685a3790e057c4989b65f0ac5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 25 Oct 2025 16:49:55 +0200 Subject: [PATCH] client: Client.ContainerStats: close reader on context cancellation Use a cancelReadCloser to automatically close the reader when the context is cancelled. Consumers are still recommended to manually close the reader, but the cancelReadCloser makes the Close idempotent. Signed-off-by: Sebastiaan van Stijn --- client/container_stats.go | 4 +++- vendor/github.com/moby/moby/client/container_stats.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/client/container_stats.go b/client/container_stats.go index 6a4b1c3b6c..277769dbff 100644 --- a/client/container_stats.go +++ b/client/container_stats.go @@ -43,6 +43,8 @@ type ContainerStatsResult struct { // ContainerStats retrieves live resource usage statistics for the specified // container. The caller must close the [io.ReadCloser] in the returned result // to release associated resources. +// +// The underlying [io.ReadCloser] is automatically closed if the context is canceled, func (cli *Client) ContainerStats(ctx context.Context, containerID string, options ContainerStatsOptions) (ContainerStatsResult, error) { containerID, err := trimID("container", containerID) if err != nil { @@ -68,6 +70,6 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, optio } return ContainerStatsResult{ - Body: resp.Body, + Body: newCancelReadCloser(ctx, resp.Body), }, nil } diff --git a/vendor/github.com/moby/moby/client/container_stats.go b/vendor/github.com/moby/moby/client/container_stats.go index 6a4b1c3b6c..277769dbff 100644 --- a/vendor/github.com/moby/moby/client/container_stats.go +++ b/vendor/github.com/moby/moby/client/container_stats.go @@ -43,6 +43,8 @@ type ContainerStatsResult struct { // ContainerStats retrieves live resource usage statistics for the specified // container. The caller must close the [io.ReadCloser] in the returned result // to release associated resources. +// +// The underlying [io.ReadCloser] is automatically closed if the context is canceled, func (cli *Client) ContainerStats(ctx context.Context, containerID string, options ContainerStatsOptions) (ContainerStatsResult, error) { containerID, err := trimID("container", containerID) if err != nil { @@ -68,6 +70,6 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, optio } return ContainerStatsResult{ - Body: resp.Body, + Body: newCancelReadCloser(ctx, resp.Body), }, nil }