From 623c3fee2d7b6060ea8dfa9385a59e16f87af5ce Mon Sep 17 00:00:00 2001 From: Lucas Winther Date: Mon, 23 Nov 2026 23:13:48 +0100 Subject: [PATCH] protondrive: stop a misbehaving server from making one events poll run forever Read at most 100 pages of events per poll; the rest are read by the next poll. Co-Authored-By: Claude Opus 5.5 --- backend/protondrive/events.go | 10 ++++--- backend/protondrive/events_internal_test.go | 30 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/backend/protondrive/events.go b/backend/protondrive/events.go index 9bf3af3a7..7e99b8319 100644 --- a/backend/protondrive/events.go +++ b/backend/protondrive/events.go @@ -87,15 +87,19 @@ func (c *client) latestEventID(ctx context.Context) (string, error) { return resp.EventID, nil } +// maxEventPages is the most pages of events one poll reads; the rest +// are read by the next poll. +const maxEventPages = 100 + // pollEvents returns the changes after event cursor, oldest first, and -// the event ID to poll from next, reading every page there is. +// the event ID to poll from next, reading up to maxEventPages pages. // // If the server no longer knows cursor it returns refresh set, no // changes and the newest event ID: anything cached may be out of date. // On error it returns no changes, and the poll should be retried from // cursor. It wraps errVolumeGone if the volume is gone. func (c *client) pollEvents(ctx context.Context, cursor string) (changes []eventChange, next string, refresh bool, err error) { - for { + for pages := 1; ; pages++ { opts := &rest.Opts{ Method: "GET", Path: c.volumePath("/drive/v2/volumes", "events", cursor), @@ -118,7 +122,7 @@ func (c *client) pollEvents(ctx context.Context, cursor string) (changes []event if page.More && len(page.Events) > 0 && page.EventID == cursor { fs.Debugf(nil, "protondrive: events: page after %s doesn't advance: its events will be read again", cursor) } - if !page.More || len(page.Events) == 0 || page.EventID == cursor { + if !page.More || len(page.Events) == 0 || page.EventID == cursor || pages >= maxEventPages { return changes, page.EventID, false, nil } cursor = page.EventID diff --git a/backend/protondrive/events_internal_test.go b/backend/protondrive/events_internal_test.go index f8b90a9e1..3ea13948d 100644 --- a/backend/protondrive/events_internal_test.go +++ b/backend/protondrive/events_internal_test.go @@ -3,6 +3,7 @@ package protondrive import ( "context" "errors" + "fmt" "io" "log/slog" "net/http" @@ -226,6 +227,35 @@ func TestPollEventsHostilePages(t *testing.T) { } } +func TestPollEventsEndlessPages(t *testing.T) { + t.Parallel() + ctx := context.Background() + srv := protontest.New(t) + u := srv.AddUser("user", "pass") + var pages int + c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, func(next http.RoundTripper) http.RoundTripper { + return roundTripFunc(func(r *http.Request) (*http.Response, error) { + if !strings.Contains(r.URL.Path, "/events/") || strings.HasSuffix(r.URL.Path, "/latest") { + return next.RoundTrip(r) + } + pages++ + body := fmt.Sprintf(`{"Code":1000,"Events":[{"EventID":"e%d","EventType":2,"Link":{"LinkID":"a"}}],"EventID":"p%d","More":true}`, pages, pages) + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": {"application/json"}}, + Body: io.NopCloser(strings.NewReader(body)), + Request: r, + }, nil + }) + }) + changes, next, refresh, err := c.pollEvents(ctx, "start") + require.NoError(t, err) + assert.False(t, refresh) + assert.Equal(t, maxEventPages, pages) + assert.Len(t, changes, maxEventPages) + assert.Equal(t, fmt.Sprintf("p%d", maxEventPages), next) +} + // eventsTestClient returns a client for no server whose cache holds // root/{dir/{sub/{deep}, file}, top, other/}, with every listing. func eventsTestClient() *client {