From 9060bbe0313da9378adf2cb2ae51694cbc08ee60 Mon Sep 17 00:00:00 2001 From: "hiroto.toyoda" Date: Thu, 22 Oct 2026 18:10:58 +0900 Subject: [PATCH] fix(down): do not remove a volume whose ownership cannot be checked Signed-off-by: hiroto.toyoda --- pkg/compose/down.go | 6 +++++- pkg/compose/down_test.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/compose/down.go b/pkg/compose/down.go index 44393ae33..a31419639 100644 --- a/pkg/compose/down.go +++ b/pkg/compose/down.go @@ -388,10 +388,14 @@ func (s *composeService) removeVolume(ctx context.Context, id string, projectNam // Already gone return nil } + if err != nil { + // without the inspect result its owner can't be checked: keep it + return fmt.Errorf("failed to inspect volume %s: %w", id, err) + } // A volume declared with an explicit name can belong to another project // sharing that name, or have been created outside Compose: up only reuses // such a volume (see ownedConfigHash), so down must not remove it either. - if owner := res.Volume.Labels[api.ProjectLabel]; err == nil && owner != projectName { + if owner := res.Volume.Labels[api.ProjectLabel]; owner != projectName { reason := fmt.Sprintf("Volume belongs to project %q", owner) if owner == "" { reason = "Volume was not created by Docker Compose" diff --git a/pkg/compose/down_test.go b/pkg/compose/down_test.go index 67b5b7809..29678f348 100644 --- a/pkg/compose/down_test.go +++ b/pkg/compose/down_test.go @@ -537,6 +537,29 @@ func TestEnsureVolumesDown_SkipsVolumeNotOwnedByProject(t *testing.T) { }) } +// TestEnsureVolumesDown_KeepsVolumeWhenInspectFails guards #14373: a volume +// whose owner can't be checked must not be removed. +func TestEnsureVolumesDown_KeepsVolumeWhenInspectFails(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + apiClient, cli := prepareMocks(mockCtrl) + svcIface, err := NewComposeService(cli) + assert.NilError(t, err) + svc := svcIface.(*composeService) + + project := &types.Project{Name: "prj", Volumes: types.Volumes{ + "data": {Name: "prj-data"}, + }} + apiClient.EXPECT().VolumeInspect(gomock.Any(), "prj-data", gomock.Any()). + Return(client.VolumeInspectResult{}, errors.New("engine unavailable")) + // no VolumeRemove expectation — a call for prj-data fails the test + + ops := svc.ensureVolumesDown(t.Context(), project, nil) + assert.Equal(t, len(ops), 1) + assert.Error(t, ops[0](), "failed to inspect volume prj-data: engine unavailable") +} + func TestDownRemoveImages(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish()