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]>
This commit is contained in:
Milas Bowman
2023-04-12 12:15:58 -04:00
committed by GitHub
parent 1fb0c03e8b
commit 9ef173a3ac
+10 -6
View File
@@ -22,6 +22,8 @@ import (
"strings"
"time"
"github.com/docker/docker/errdefs"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"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{
Services: services,
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)
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
}
container := moby.Container{