Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(wiki): release inflight slot on task cancellation
kerthansafeee71e4807
diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.goindex 1e2ea5f83..4805899e8 100644--- a/internal/application/service/wiki_ingest.go+++ b/internal/application/service/wiki_ingest.go@@ -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 stringreturn 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}diff --git a/internal/application/service/wiki_ingest_test.go b/internal/application/service/wiki_ingest_test.goindex 091927df9..e0815f53f 100644--- a/internal/application/service/wiki_ingest_test.go+++ b/internal/application/service/wiki_ingest_test.go@@ -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