fix(im): recover from deleted session when GetSession returns app sentinel (#1499)
The recovery branch added in c578fdba (for #1046) checked
`errors.Is(err, gorm.ErrRecordNotFound)`, but the session repository
translates the GORM error into `apperrors.ErrSessionNotFound` before it
reaches the IM service. The predicate therefore never matched, the
stale ChannelSession mapping was never recycled, and the bot stayed
permanently unresponsive after the underlying session was deleted from
the UI — exactly the symptom reported in #1499.
Extract the predicate into `isSessionNotFound` that matches both
sentinels (application sentinel as actually returned today; GORM
sentinel kept as a safety net against future repository reverts), and
add a focused regression test guarding the invariant.
Refs: https://github.com/Tencent/WeKnora/issues/1499
This commit is contained in:
+12
-2
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
filesvc "github.com/Tencent/WeKnora/internal/application/service/file"
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
@@ -1048,8 +1049,8 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
// ChannelSession mapping still exists (GORM soft-delete does not trigger
|
||||
// SQL ON DELETE CASCADE). Recover by soft-deleting the stale mapping and
|
||||
// re-creating a fresh session so the IM bot doesn't become permanently
|
||||
// unresponsive. (fixes #1046)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// unresponsive. (fixes #1046, #1499)
|
||||
if isSessionNotFound(err) {
|
||||
logger.Warnf(ctx, "[IM] Session %s not found (deleted?), recycling stale channel session %s",
|
||||
channelSession.SessionID, channelSession.ID)
|
||||
if delErr := s.db.Delete(&ChannelSession{}, "id = ?", channelSession.ID).Error; delErr != nil {
|
||||
@@ -1324,6 +1325,15 @@ func (s *Service) sendStreamReply(ctx context.Context, msg *IncomingMessage, str
|
||||
return nil
|
||||
}
|
||||
|
||||
// isSessionNotFound reports whether err indicates the underlying WeKnora
|
||||
// session no longer exists. The session repository translates GORM's
|
||||
// ErrRecordNotFound into apperrors.ErrSessionNotFound, so the application
|
||||
// sentinel is what GetSession returns today; the GORM check is kept as a
|
||||
// safety net in case a future repository revert bypasses the translation.
|
||||
func isSessionNotFound(err error) bool {
|
||||
return errors.Is(err, apperrors.ErrSessionNotFound) || errors.Is(err, gorm.ErrRecordNotFound)
|
||||
}
|
||||
|
||||
// resolveSession dispatches to the appropriate session resolution strategy
|
||||
// based on the channel's session mode.
|
||||
func (s *Service) resolveSession(ctx context.Context, msg *IncomingMessage, tenantID uint64, agentID string, imChannelID string, sessionMode string) (*ChannelSession, error) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package im
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestIsSessionNotFound guards the recovery path for issue #1499.
|
||||
//
|
||||
// The session repository translates gorm.ErrRecordNotFound into
|
||||
// apperrors.ErrSessionNotFound, so an `errors.Is(err, gorm.ErrRecordNotFound)`
|
||||
// check on the value returned by SessionService.GetSession would silently
|
||||
// miss — leaving the IM bot permanently unresponsive after the user deletes
|
||||
// the underlying session from the UI.
|
||||
func TestIsSessionNotFound(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{"app sentinel as returned by sessionService.GetSession today", apperrors.ErrSessionNotFound, true},
|
||||
{"wrapped app sentinel", fmt.Errorf("get session: %w", apperrors.ErrSessionNotFound), true},
|
||||
{"raw gorm sentinel (safety net)", gorm.ErrRecordNotFound, true},
|
||||
{"wrapped gorm sentinel", fmt.Errorf("query session: %w", gorm.ErrRecordNotFound), true},
|
||||
{"unrelated error", errors.New("boom"), false},
|
||||
{"nil", nil, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isSessionNotFound(tt.err); got != tt.want {
|
||||
t.Errorf("isSessionNotFound(%v) = %v, want %v", tt.err, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestErrSessionNotFoundIsNotGormErrRecordNotFound documents the invariant
|
||||
// that motivated the bug: the two sentinels are distinct error values, so
|
||||
// callers must explicitly match the application sentinel.
|
||||
func TestErrSessionNotFoundIsNotGormErrRecordNotFound(t *testing.T) {
|
||||
if errors.Is(apperrors.ErrSessionNotFound, gorm.ErrRecordNotFound) {
|
||||
t.Fatal("apperrors.ErrSessionNotFound must not unwrap to gorm.ErrRecordNotFound; " +
|
||||
"if this changes, the IM recovery path can be simplified accordingly")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user