From 986a6f36995d689d638394178ae22a4ff27d757c Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 23 Jul 2026 12:19:21 +0800 Subject: [PATCH] fix(tasks): detach KB deletion queue cleanup from request ctx Run the synchronous best-effort queue scrub in DeleteKnowledgeBase with context.WithoutCancel and kbTaskCleanupTimeout so client disconnects cannot truncate cancellation mid-scan, matching ProcessKBDelete cleanup semantics. --- internal/application/service/knowledgebase.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/application/service/knowledgebase.go b/internal/application/service/knowledgebase.go index 5eb8f0e9..931de9fa 100644 --- a/internal/application/service/knowledgebase.go +++ b/internal/application/service/knowledgebase.go @@ -731,7 +731,16 @@ func (s *knowledgeBaseService) DeleteKnowledgeBase(ctx context.Context, id strin // Stop both ephemeral queue work and durable wiki operations that target // the now-deleted KB. ProcessKBDelete repeats this with document IDs and // performs one final scrub after heavy cleanup to close enqueue races. - s.cleanupTasksForKnowledgeBase(ctx, id, nil, nil) + // + // Run detached with a bounded timeout so a disconnecting API client cannot + // truncate this best-effort scrub mid-scan, matching ProcessKBDelete's + // cleanup semantics. The KB row is already soft-deleted, so the async + // delete task remains the durable backstop even if this pass is cut short. + kbCleanupCtx, cancelKBCleanup := context.WithTimeout( + context.WithoutCancel(ctx), kbTaskCleanupTimeout, + ) + s.cleanupTasksForKnowledgeBase(kbCleanupCtx, id, nil, nil) + cancelKBCleanup() // Step 1b: Remove all organization shares for this KB so org settings no longer show them if s.shareRepo != nil { @@ -744,7 +753,11 @@ func (s *knowledgeBaseService) DeleteKnowledgeBase(ctx context.Context, id strin // schedules and in-flight sync logs do not keep running against a deleted KB. dataSourceIDs := s.deleteDataSourcesForKnowledgeBase(ctx, id) if len(dataSourceIDs) > 0 { - s.cancelTasksForKnowledgeBase(ctx, id, nil, dataSourceIDs) + dsCancelCtx, cancelDSCancel := context.WithTimeout( + context.WithoutCancel(ctx), kbTaskCleanupTimeout, + ) + s.cancelTasksForKnowledgeBase(dsCancelCtx, id, nil, dataSourceIDs) + cancelDSCancel() } // Step 2: Enqueue async task for heavy cleanup operations