fix(helps): release TransportCache mutex on builder panic and retire purged entries

Ensure TransportCache.Get and detachMatching always release c.mu via defer
when a caller-supplied callback panics, preventing the cache mutex from being
permanently wedged after recovery. Also mark entries as retired in Purge() to
match evictLocked, CloseKey, and detachMatching.

Fixes #7162
This commit is contained in:
sususu
2026-11-01 20:07:46 +08:00
parent a21814ec60
commit 8a82bed582
2 changed files with 57 additions and 5 deletions

View File

@@ -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)

View File

@@ -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")
}
}