mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
fix(usage): clear consumed queue slot in usage manager dispatcher
- Zero the dequeued queueItem slot in Manager.run before reslicing the queue so the backing array does not retain references to consumed request contexts and usage records while idle. - Add a weak-pointer garbage collection regression test verifying that consumed request context payloads and record reference fields are reclaimed when the manager is idle. Closes: #7267
This commit is contained in:
@@ -779,6 +779,7 @@ func (m *Manager) run(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
item := m.queue[0]
|
||||
m.queue[0] = queueItem{}
|
||||
m.queue = m.queue[1:]
|
||||
m.mu.Unlock()
|
||||
m.dispatch(item)
|
||||
|
||||
@@ -2,8 +2,11 @@ package usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
"weak"
|
||||
)
|
||||
|
||||
func TestStreamFromContextDefaultsMissingToFalse(t *testing.T) {
|
||||
@@ -186,3 +189,70 @@ func TestManagerPublishBackfillsContextAuthFields(t *testing.T) {
|
||||
t.Fatal("timed out waiting for usage record")
|
||||
}
|
||||
}
|
||||
|
||||
type notifyUsagePlugin struct {
|
||||
delivered chan struct{}
|
||||
}
|
||||
|
||||
func (p *notifyUsagePlugin) HandleUsage(_ context.Context, _ Record) {
|
||||
p.delivered <- struct{}{}
|
||||
}
|
||||
|
||||
type retainedRequestPayload struct {
|
||||
data [64 << 10]byte
|
||||
}
|
||||
|
||||
type retainedRequestContextKey struct{}
|
||||
|
||||
//go:noinline
|
||||
func publishConsumableRecord(mgr *Manager) (weak.Pointer[retainedRequestPayload], weak.Pointer[CacheDetails], weak.Pointer[string]) {
|
||||
ctxPayload := &retainedRequestPayload{}
|
||||
cacheDetails := &CacheDetails{}
|
||||
headerSlice := []string{"req-header-value"}
|
||||
|
||||
ctx := context.WithValue(context.Background(), retainedRequestContextKey{}, ctxPayload)
|
||||
mgr.Publish(ctx, Record{
|
||||
Provider: "openai",
|
||||
Model: "gpt-5.4",
|
||||
ResponseHeaders: http.Header{"X-Request-Id": headerSlice},
|
||||
Detail: Detail{
|
||||
CacheDetails: cacheDetails,
|
||||
},
|
||||
})
|
||||
|
||||
return weak.Make(ctxPayload), weak.Make(cacheDetails), weak.Make(&headerSlice[0])
|
||||
}
|
||||
|
||||
func TestManagerReleasesConsumedQueueItemReferencesWhenIdle(t *testing.T) {
|
||||
mgr := NewManager(10)
|
||||
t.Cleanup(mgr.Stop)
|
||||
|
||||
plugin := ¬ifyUsagePlugin{delivered: make(chan struct{}, 1)}
|
||||
mgr.Register(plugin)
|
||||
|
||||
ctxWeak, detailWeak, headerWeak := publishConsumableRecord(mgr)
|
||||
|
||||
select {
|
||||
case <-plugin.delivered:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for usage record dispatch")
|
||||
}
|
||||
|
||||
for range 10 {
|
||||
runtime.Gosched()
|
||||
runtime.GC()
|
||||
if ctxWeak.Value() == nil && detailWeak.Value() == nil && headerWeak.Value() == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if ctxWeak.Value() != nil {
|
||||
t.Fatalf("consumed request context payload is still retained while manager is idle")
|
||||
}
|
||||
if detailWeak.Value() != nil {
|
||||
t.Fatalf("consumed record Detail.CacheDetails is still retained while manager is idle")
|
||||
}
|
||||
if headerWeak.Value() != nil {
|
||||
t.Fatalf("consumed record ResponseHeaders backing value is still retained while manager is idle")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user