Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func New(client *http.Client) *Fetcher {

// Get fetches the given url.
func (f *Fetcher) Get(ctx context.Context, url string) (*Result, error) {
fetchCtx := context.WithoutCancel(ctx)
v, err, _ := f.group.Do(url, func() (any, error) {
return f.doGet(ctx, url)
return f.doGet(fetchCtx, url)
})
if err != nil {
return nil, err
Expand Down
48 changes: 48 additions & 0 deletions internal/upstream/upstream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2026 Edgeless Systems GmbH
// SPDX-License-Identifier: BUSL-1.1

package upstream

import (
"context"
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestCallerCancellationDoesNotAbortFetch covers the case of a client disconnecting while the upstream request it triggered is still in flight.
func TestCallerCancellationDoesNotAbortFetch(t *testing.T) {
release := make(chan struct{})
var hits int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
<-release
select {
case <-r.Context().Done():
// The server saw the fetch get canceled; fail via an empty body below.
return
default:
}
_, _ = w.Write([]byte("collateral"))
}))
defer srv.Close()

f := New(srv.Client())

canceledCtx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Go(func() {
res, err := f.Get(canceledCtx, srv.URL)
require.NoError(t, err)
assert.Equal(t, "collateral", string(res.Body))
})

cancel()
close(release)
wg.Wait()
assert.Equal(t, 1, hits)
}
Loading