Browse Source

log: fix race on container kill (#10459)

If we go to inspect a container that we got an event for and it
no longer exists on the server, handle clean up without erroring
out.

Fixes #10373.

Signed-off-by: Milas Bowman <[email protected]>
Milas Bowman 2 năm trước cách đây
mục cha
commit
9ef173a3ac
1 tập tin đã thay đổi với 10 bổ sung6 xóa
  1. 10 6
      pkg/compose/start.go

+ 10 - 6
pkg/compose/start.go

@@ -22,6 +22,8 @@ import (
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
+	"github.com/docker/docker/errdefs"
+
 	"github.com/docker/compose/v2/pkg/api"
 	"github.com/docker/compose/v2/pkg/api"
 	"github.com/docker/compose/v2/pkg/progress"
 	"github.com/docker/compose/v2/pkg/progress"
 	"github.com/docker/compose/v2/pkg/utils"
 	"github.com/docker/compose/v2/pkg/utils"
@@ -169,14 +171,16 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
 	err := s.Events(ctx, projectName, api.EventsOptions{
 	err := s.Events(ctx, projectName, api.EventsOptions{
 		Services: services,
 		Services: services,
 		Consumer: func(event api.Event) error {
 		Consumer: func(event api.Event) error {
-			if event.Status == "destroy" {
-				// This container can't be inspected, because it's gone.
-				// It's already been removed from the watched map.
-				return nil
-			}
-
 			inspected, err := s.apiClient().ContainerInspect(ctx, event.Container)
 			inspected, err := s.apiClient().ContainerInspect(ctx, event.Container)
 			if err != nil {
 			if err != nil {
+				if errdefs.IsNotFound(err) {
+					// it's possible to get "destroy" or "kill" events but not
+					// be able to inspect in time before they're gone from the
+					// API, so just remove the watch without erroring
+					delete(watched, event.Container)
+					expected = utils.Remove(expected, event.Container)
+					return nil
+				}
 				return err
 				return err
 			}
 			}
 			container := moby.Container{
 			container := moby.Container{