From 57c8809f2740a1850559efdbeee2b5d49d45fbeb Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 22 Nov 2026 13:57:46 +0100 Subject: [PATCH] Detach a coalesced manifest fetch only when a detach slot is free coalescedManifest always ran the shared fetch on a context detached from its first caller. It now takes one of the maxDetachedMetadataFetches slots first, as coalesceMetadata does, and otherwise ends with that caller, so clients that request uncached manifests and hang up can't leave any number of upstream fetches running. --- .../handler/container_manifest_detach_test.go | 104 ++++++++++++++++++ internal/handler/container_manifest_fetch.go | 15 ++- 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 internal/handler/container_manifest_detach_test.go diff --git a/internal/handler/container_manifest_detach_test.go b/internal/handler/container_manifest_detach_test.go new file mode 100644 index 00000000..dc105ed9 --- /dev/null +++ b/internal/handler/container_manifest_detach_test.go @@ -0,0 +1,104 @@ +package handler + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +// hangingRegistry holds every manifest request until the request is +// cancelled or a while has passed, and reports which came first. +func hangingRegistry(t *testing.T, wait time.Duration) (*ContainerHandler, <-chan struct{}, <-chan bool) { + t.Helper() + entered := make(chan struct{}, 1) + cancelled := make(chan bool, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + select { + case entered <- struct{}{}: + default: + } + select { + case <-r.Context().Done(): + cancelled <- true + case <-time.After(wait): + cancelled <- false + http.NotFound(w, r) + } + })) + t.Cleanup(server.Close) + proxy, _, _, _ := setupTestProxy(t) + proxy.HTTPClient = server.Client() + proxy.MetadataTTL = time.Hour + h := &ContainerHandler{proxy: proxy, registryURL: server.URL, proxyURL: "http://localhost:8080"} + return h, entered, cancelled +} + +// pullAndHangUp requests a manifest and cancels the request once it reached +// upstream. +func pullAndHangUp(t *testing.T, h *ContainerHandler, entered <-chan struct{}) { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { + defer close(done) + request := httptest.NewRequest(http.MethodGet, "/library/app/manifests/latest", nil).WithContext(ctx) + request.Header.Set("Accept", dockerManifestAccept) + h.Routes().ServeHTTP(httptest.NewRecorder(), request) + }() + select { + case <-entered: + case <-time.After(5 * time.Second): + t.Fatal("the manifest request never reached upstream") + } + cancel() + <-done +} + +func TestContainerHandler_ManifestFetchOutlivesItsCaller(t *testing.T) { + h, entered, cancelled := hangingRegistry(t, 300*time.Millisecond) + pullAndHangUp(t, h, entered) + if <-cancelled { + t.Error("the upstream manifest request ended with its caller, with a detach slot free") + } +} + +// TestContainerHandler_ManifestFetchDetachCapFallsBackToClientContext checks +// that manifest fetches share the cap on detached metadata fetches: with +// every slot taken, a caller that hangs up ends its fetch. +func TestContainerHandler_ManifestFetchDetachCapFallsBackToClientContext(t *testing.T) { + h, entered, cancelled := hangingRegistry(t, 5*time.Second) + for range maxDetachedMetadataFetches { + if !h.proxy.acquireMetadataDetachSlot() { + t.Fatal("could not take a free metadata detach slot") + } + } + defer func() { + for range maxDetachedMetadataFetches { + h.proxy.releaseMetadataDetachSlot() + } + }() + pullAndHangUp(t, h, entered) + if !<-cancelled { + t.Error("the upstream manifest request kept running after its caller left, with no slot free") + } +} + +func TestContainerHandler_ManifestFetchReleasesItsDetachSlot(t *testing.T) { + h, entered, cancelled := hangingRegistry(t, 50*time.Millisecond) + for range maxDetachedMetadataFetches - 1 { + if !h.proxy.acquireMetadataDetachSlot() { + t.Fatal("could not take a free metadata detach slot") + } + } + pullAndHangUp(t, h, entered) + <-cancelled + deadline := time.Now().Add(5 * time.Second) + for !h.proxy.acquireMetadataDetachSlot() { + if time.Now().After(deadline) { + t.Fatal("the manifest fetch kept its detach slot after it ended") + } + time.Sleep(10 * time.Millisecond) + } +} diff --git a/internal/handler/container_manifest_fetch.go b/internal/handler/container_manifest_fetch.go index fc88e1d7..2abaacff 100644 --- a/internal/handler/container_manifest_fetch.go +++ b/internal/handler/container_manifest_fetch.go @@ -99,12 +99,19 @@ func (c *capturedResponse) write(p *Proxy, w http.ResponseWriter, r *http.Reques // coalescedManifest runs the upstream part of a manifest request once for // concurrent callers with the same key. Like coalesceMetadata, it runs on a -// context detached from the first caller, and each waiter still leaves when -// its own context ends. The result is a whole manifest with its headers, -// which a metadataResult does not carry, hence the separate group. +// context detached from the first caller when one of the +// maxDetachedMetadataFetches slots is free, and ends with that caller +// otherwise; each waiter still leaves when its own context ends. The result +// is a whole manifest with its headers, which a metadataResult does not +// carry, hence the separate group. func (h *ContainerHandler) coalescedManifest(ctx context.Context, req manifestRequest) manifestOutcome { results := h.manifestFlight.DoChan(req.coalesceKey(), func() (any, error) { - return h.fetchManifest(context.WithoutCancel(ctx), req), nil + fetchCtx := ctx + if h.proxy.acquireMetadataDetachSlot() { + defer h.proxy.releaseMetadataDetachSlot() + fetchCtx = context.WithoutCancel(ctx) + } + return h.fetchManifest(fetchCtx, req), nil }) select { case <-ctx.Done():