fix(wiki): release inflight slot on task cancellation

main
kerthans 2026-09-21 12:20:29 +08:00 committed by lyingbug
parent 7feac9c570
commit afeee71e48
2 changed files with 35 additions and 7 deletions

View File

@ -961,11 +961,13 @@ return 1
// standard/Redis mode). Returns (release, true) when granted — release() MUST
// run when the batch finishes; (nil, false) when the KB is already at
// maxInflight, so the caller should reschedule and bail. A background renew
// keeps the slot alive for the batch's duration; a crashed batch's slot simply
// expires (wikiInflightTTL) and is purged by the next reserver. Lite mode has
// no shared-pool contention (liteLocks already serialize per KB), so it always
// grants a no-op slot. Fails OPEN on a Redis error: a blip must not halt wiki
// generation, and the pool size still bounds total work.
// keeps the slot alive for the batch's duration and removes it when the task
// context is canceled, even if the handler itself is still blocked. A crashed
// batch's slot simply expires (wikiInflightTTL) and is purged by the next
// reserver. Lite mode has no shared-pool contention (liteLocks already
// serialize per KB), so it always grants a no-op slot. Fails OPEN on a Redis
// error: a blip must not halt wiki generation, and the pool size still bounds
// total work.
func (s *wikiIngestService) reserveInflightSlot(ctx context.Context, kbID string, maxInflight int) (func(), bool) {
if s.redisClient == nil || maxInflight <= 0 {
return func() {}, true
@ -989,10 +991,13 @@ func (s *wikiIngestService) reserveInflightSlot(ctx context.Context, kbID string
return nil, false
}
renewCtx, cancel := context.WithCancel(context.Background())
renewCtx, cancel := context.WithCancel(ctx)
renewDone := make(chan struct{})
go func() {
ticker := time.NewTicker(wikiInflightRenew)
defer ticker.Stop()
defer close(renewDone)
defer s.redisClient.ZRem(context.Background(), key, token)
for {
select {
case <-renewCtx.Done():
@ -1006,7 +1011,7 @@ func (s *wikiIngestService) reserveInflightSlot(ctx context.Context, kbID string
}()
return func() {
cancel()
s.redisClient.ZRem(context.Background(), key, token)
<-renewDone
}, true
}

View File

@ -9,11 +9,34 @@ import (
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"github.com/Tencent/WeKnora/internal/agent"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
)
func TestWikiInflightSlotReleasedWhenTaskContextCanceled(t *testing.T) {
mr := miniredis.RunT(t)
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
t.Cleanup(func() { _ = rdb.Close() })
svc := &wikiIngestService{redisClient: rdb}
ctx, cancel := context.WithCancel(context.Background())
release, granted := svc.reserveInflightSlot(ctx, "kb-1", 1)
require.True(t, granted)
t.Cleanup(release)
require.Equal(t, int64(1), rdb.ZCard(context.Background(), wikiInflightPrefix+"kb-1").Val())
cancel()
require.Eventually(t, func() bool {
return rdb.ZCard(context.Background(), wikiInflightPrefix+"kb-1").Val() == 0
}, time.Second, 10*time.Millisecond, "canceled tasks must not retain an inflight slot")
}
func TestSlugify(t *testing.T) {
tests := []struct {
input string