client: Client.ContainerExport: 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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-30 09:55:45 +01:00
parent 08cd02cab6
commit c5aedacb4f
7 changed files with 27 additions and 53 deletions

View File

@@ -103,11 +103,11 @@ func (s *DockerAPISuite) TestContainerAPIGetExport(c *testing.T) {
assert.NilError(c, err)
defer apiClient.Close()
body, err := apiClient.ContainerExport(testutil.GetContext(c), name, client.ContainerExportOptions{})
res, err := apiClient.ContainerExport(testutil.GetContext(c), name, client.ContainerExportOptions{})
assert.NilError(c, err)
defer body.Close()
defer res.Close()
found := false
for tarReader := tar.NewReader(body); ; {
for tarReader := tar.NewReader(res); ; {
h, err := tarReader.Next()
if errors.Is(err, io.EOF) {
break