From b6337cd970de79d604b249a62c8ace53d565099a Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 3 Jun 2026 11:30:16 +0800 Subject: [PATCH] fix(session): handle user-requested stop events gracefully in QA execution - Updated the QA execution logic to differentiate between user-requested cancellations and actual errors, preventing unnecessary error events from being emitted when a stop is requested. - Enhanced logging to provide clear feedback when a QA session is cancelled by the user, improving the user experience and reducing confusion. - Added context cancellation checks to ensure proper handling of stopped generation scenarios, maintaining the integrity of the session flow. --- .../service/session_knowledge_qa.go | 18 +++++++++++++ internal/handler/session/qa.go | 26 ++++++++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/internal/application/service/session_knowledge_qa.go b/internal/application/service/session_knowledge_qa.go index 3b6c0d53..6b8c25c6 100644 --- a/internal/application/service/session_knowledge_qa.go +++ b/internal/application/service/session_knowledge_qa.go @@ -590,6 +590,24 @@ func (s *sessionService) KnowledgeQAByEvent(ctx context.Context, }, nil, spanErr) } + // If the user stopped generation, the context is cancelled. A cancelled + // retrieval stage surfaces as ErrSearchNothing (the search goroutines + // return no results when their embedding/vector calls are aborted), so + // this check MUST come before the ErrSearchNothing handling below. + // Otherwise we would persist the fixed fallback response ("Sorry, I am + // unable to answer this question.") over the intentionally-empty stopped + // message, and the user would see the fallback text after refreshing. + // This is not single-machine specific: the stop arrives via the shared + // StreamManager and cancels asyncCtx on whichever node is generating. + if ctxErr := ctx.Err(); ctxErr != nil { + common.PipelineWarn(ctx, "Pipeline", "stage_cancelled", map[string]interface{}{ + "event": string(eventType), + "duration_ms": stageDuration.Milliseconds(), + "reason": ctxErr.Error(), + }) + return ctxErr + } + if err == chatpipeline.ErrSearchNothing { common.PipelineWarn(ctx, "Pipeline", "stage_fallback", map[string]interface{}{ "event": string(eventType), diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go index 7caf2ff2..a8dd9b22 100644 --- a/internal/handler/session/qa.go +++ b/internal/handler/session/qa.go @@ -764,16 +764,24 @@ func (h *Handler) executeQA(reqCtx *qaRequestContext, mode qaMode, generateTitle } if serviceErr != nil { - logger.ErrorWithFields(streamCtx.asyncCtx, serviceErr, nil) - streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{ - Type: event.EventError, - SessionID: sessionID, - Data: event.ErrorData{ - Error: serviceErr.Error(), - Stage: stageName, + // A user-requested stop cancels asyncCtx, which surfaces here as a + // context cancellation. That is an expected outcome, not a failure: + // the stop event already notifies the client, so don't emit a + // spurious error event (which would otherwise show an error toast). + if streamCtx.asyncCtx.Err() != nil { + logger.Infof(streamCtx.asyncCtx, "QA cancelled by user stop for session: %s", sessionID) + } else { + logger.ErrorWithFields(streamCtx.asyncCtx, serviceErr, nil) + streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{ + Type: event.EventError, SessionID: sessionID, - }, - }) + Data: event.ErrorData{ + Error: serviceErr.Error(), + Stage: stageName, + SessionID: sessionID, + }, + }) + } } }()