From a74e89a7ccd258128743f11bf64adb5cdd3d8a19 Mon Sep 17 00:00:00 2001 From: Zhuoming Wang Date: Tue, 3 Nov 2026 20:31:39 +0800 Subject: [PATCH] fix(events): treat io.EOF as a normal end of a bounded stream The Engine client sends io.EOF over EventsResult.Err once the stream has been read completely, which is what happens when `events --until` reaches its deadline. Service.Events returned that error verbatim, so the command printed "EOF" and exited 1 even though every event had been delivered. Return nil for io.EOF and keep propagating real stream errors. Signed-off-by: Zhuoming Wang --- internal/inspect/events.go | 8 ++++ internal/inspect/events_test.go | 69 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 internal/inspect/events_test.go diff --git a/internal/inspect/events.go b/internal/inspect/events.go index 38fa48eae..7085bf3bd 100644 --- a/internal/inspect/events.go +++ b/internal/inspect/events.go @@ -18,6 +18,8 @@ package inspect import ( "context" + "errors" + "io" "slices" "strings" "time" @@ -76,6 +78,12 @@ func (s *Service) Events(ctx context.Context, projectName string, options api.Ev } case err := <-res.Err: + // A bounded stream (e.g. events --until) ends with io.EOF once the + // daemon has sent every matching event. That is a normal completion, + // not a command failure. + if errors.Is(err, io.EOF) { + return nil + } return err } } diff --git a/internal/inspect/events_test.go b/internal/inspect/events_test.go new file mode 100644 index 000000000..0aa34c28e --- /dev/null +++ b/internal/inspect/events_test.go @@ -0,0 +1,69 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package inspect + +import ( + "context" + "errors" + "io" + "testing" + + "github.com/moby/moby/api/types/events" + "github.com/moby/moby/client" + "go.uber.org/mock/gomock" + "gotest.tools/v3/assert" + + "github.com/docker/compose/v5/pkg/api" + "github.com/docker/compose/v5/pkg/mocks" +) + +func TestEventsTreatsEOFAsCompletion(t *testing.T) { + ctrl := gomock.NewController(t) + apiClient := mocks.NewMockAPIClient(ctrl) + streamErr := make(chan error, 1) + streamErr <- io.EOF + apiClient.EXPECT().Events(gomock.Any(), gomock.Any()).Return(client.EventsResult{ + Messages: make(chan events.Message), + Err: streamErr, + }) + service := New(&fakeBackend{apiClient: apiClient}) + + err := service.Events(context.Background(), "project", api.EventsOptions{ + Consumer: func(api.Event) error { return nil }, + }) + + assert.NilError(t, err) +} + +func TestEventsPropagatesStreamErrors(t *testing.T) { + ctrl := gomock.NewController(t) + apiClient := mocks.NewMockAPIClient(ctrl) + streamErr := make(chan error, 1) + boom := errors.New("stream failed") + streamErr <- boom + apiClient.EXPECT().Events(gomock.Any(), gomock.Any()).Return(client.EventsResult{ + Messages: make(chan events.Message), + Err: streamErr, + }) + service := New(&fakeBackend{apiClient: apiClient}) + + err := service.Events(context.Background(), "project", api.EventsOptions{ + Consumer: func(api.Event) error { return nil }, + }) + + assert.Equal(t, err, boom) +}