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.
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user