diff --git a/internal/runtime/executor/helps/transport_cache.go b/internal/runtime/executor/helps/transport_cache.go index 3eec70bdff..b8225746e9 100644 --- a/internal/runtime/executor/helps/transport_cache.go +++ b/internal/runtime/executor/helps/transport_cache.go @@ -109,20 +109,23 @@ func (c *TransportCache[K]) Get(key K, build func() (*http.Transport, error)) (* } c.mu.Lock() + locked := true + defer func() { + if locked { + c.mu.Unlock() + } + }() if element, ok := c.items[key]; ok { c.order.MoveToFront(element) transport := element.Value.(*transportCacheEntry[K]).transport - c.mu.Unlock() return transport, nil } transport, errBuild := build() if errBuild != nil { - c.mu.Unlock() return nil, errBuild } if transport == nil { - c.mu.Unlock() return nil, errors.New("transport cache: build returned no transport") } @@ -130,6 +133,7 @@ func (c *TransportCache[K]) Get(key K, build func() (*http.Transport, error)) (* if element, ok := c.items[key]; ok { c.order.MoveToFront(element) existing := element.Value.(*transportCacheEntry[K]).transport + locked = false c.mu.Unlock() transport.CloseIdleConnections() return existing, nil @@ -139,6 +143,7 @@ func (c *TransportCache[K]) Get(key K, build func() (*http.Transport, error)) (* attachTransportRetirementGuard(transport, &entry.retired) c.items[key] = c.order.PushFront(entry) evicted := c.evictLocked() + locked = false c.mu.Unlock() for _, t := range evicted { @@ -219,6 +224,7 @@ func (c *TransportCache[K]) detachMatching(predicate func(key K) bool) []*http.T return nil } c.mu.Lock() + defer c.mu.Unlock() var toClose []*http.Transport var next *list.Element for element := c.order.Front(); element != nil; element = next { @@ -231,7 +237,6 @@ func (c *TransportCache[K]) detachMatching(predicate func(key K) bool) []*http.T toClose = append(toClose, entry.transport) } } - c.mu.Unlock() return toClose } @@ -253,7 +258,9 @@ func (c *TransportCache[K]) Purge() { c.mu.Lock() var toClose []*http.Transport for element := c.order.Front(); element != nil; element = element.Next() { - toClose = append(toClose, element.Value.(*transportCacheEntry[K]).transport) + entry := element.Value.(*transportCacheEntry[K]) + entry.retired.Store(true) + toClose = append(toClose, entry.transport) } c.order.Init() c.items = make(map[K]*list.Element, c.capacity) diff --git a/internal/runtime/executor/helps/transport_cache_test.go b/internal/runtime/executor/helps/transport_cache_test.go index 2bc0c39a94..0cb3ecc0af 100644 --- a/internal/runtime/executor/helps/transport_cache_test.go +++ b/internal/runtime/executor/helps/transport_cache_test.go @@ -260,3 +260,48 @@ func TestTransportCacheEvictionClosesIdleConnections(t *testing.T) { t.Fatal("expected auth-2 and auth-3 to remain in cache") } } + +func TestTransportCacheBuilderPanicReleasesMutex(t *testing.T) { + cache := NewTransportCache[cacheKey](4) + key := cacheKey{"auth-panic", ""} + + func() { + defer func() { + if r := recover(); r == nil { + t.Fatal("expected panic from builder to propagate") + } + }() + _, _ = cache.Get(key, func() (*http.Transport, error) { + panic("builder boom") + }) + }() + + if got := cache.Len(); got != 0 { + t.Fatalf("Len() after panicked build = %d, want 0", got) + } + + transport, err := cache.Get(key, func() (*http.Transport, error) { + return &http.Transport{}, nil + }) + if err != nil || transport == nil { + t.Fatalf("Get() after recovered builder panic = (%p, %v), want usable cache", transport, err) + } +} + +func TestTransportCachePurgeRetiresEntries(t *testing.T) { + cache := NewTransportCache[cacheKey](4) + key := cacheKey{"auth-purge", ""} + + _, err := cache.Get(key, func() (*http.Transport, error) { + return &http.Transport{}, nil + }) + if err != nil { + t.Fatalf("Get() error = %v", err) + } + + entry := cache.items[key].Value.(*transportCacheEntry[cacheKey]) + cache.Purge() + if !entry.retired.Load() { + t.Fatal("expected Purge() to mark removed entry as retired") + } +}