Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

4.3 KB
Raw Blame
package service

import (
	"context"
	"testing"

	"ragflow/internal/common"
	"ragflow/internal/dao"
	"ragflow/internal/engine"
	"ragflow/internal/entity"
	taskpkg "ragflow/internal/ingestion/task"
	"ragflow/internal/ingestion/testutil"
	servicepkg "ragflow/internal/service"
)

// TestExecuteMemoryTask_PanicDoesNotPropagate verifies that a panic raised inside
// the memory-extraction path (HandleSaveToMemoryTask) is recovered by
// executeMemoryTask. Before the fix, executeMemoryTask had no recover guard
// (unlike the ingestion path's settleMessage), so a panic crashed the worker
// goroutine. With concurrent_workers defaults that can be as low as 1, a
// single panicking memory task permanently removed the only worker, after which
// every normal document parse (PDF etc.) stalled in the queue — exactly the
// "parsing works at first, then breaks after some memory tasks" symptom.
//
// The test injects a panic through the runMemoryTask seam (mirrors runDocumentTask)
// so it does not depend on a live DB or a real MemoryMessageService. The contract
// asserted:
//   - executeMemoryTask returns normally (no panic propagates to the caller/worker)
//   - the MQ message is left unsettled for durable recovery
func TestExecuteMemoryTask_PanicDoesNotPropagate(t *testing.T) {
	_ = testutil.SetupTestDB(t) // ensure DB helpers are initialized; not exercised here

	ingestor := NewIngestor("test", 1, []string{"pdf"}, nil)
	// Real (non-nil) memory service so executeMemoryTask reaches the
	// runMemoryTask dispatch path instead of early-Acking on nil service.
	ingestor.SetMemoryMessageService(servicepkg.NewMemoryMessageService(servicepkg.NewMemoryService(dao.DB, engine.Get())))
	// Inject a panicking memory runner through the same seam used in production
	// (defaultRunMemoryTask calls memorySvc.HandleSaveToMemoryTask).
	ingestor.runMemoryTask = func(_ context.Context, _, _ string) (servicepkg.MemoryTaskDisposition, error) {
		panic("simulated memory extraction panic")
	}

	handle := &fakeTaskHandle{msg: common.TaskMessage{TaskID: "mem-panic-1", TaskType: common.TaskTypeMemory}}
	taskCtx := taskpkg.NewMemoryTaskContextForScheduling(context.Background(), "mem-panic-1", handle)

	// If the panic is not recovered, this call itself will panic and the test
	// fails. We also guard with a recover here only to convert a propagated
	// panic into a clear test failure (the real recovery belongs in production).
	func() {
		defer func() {
			if r := recover(); r != nil {
				t.Fatalf("executeMemoryTask let a memory panic propagate: %v", r)
			}
		}()
		ingestor.executeMemoryTask(context.Background(), taskCtx)
	}()

	if handle.nacks.Load() != 0 || handle.acks.Load() != 0 {
		t.Fatalf("panicking memory task: expected unsettled handle, got acks=%d nacks=%d", handle.acks.Load(), handle.nacks.Load())
	}
}

// TestHandleAndExecute_SurvivesMemoryPanicAndKeepsServing proves a recovered
// memory panic does not prevent the same worker from processing its next
// hand-off.
func TestHandleAndExecute_SurvivesMemoryPanicAndKeepsServing(t *testing.T) {
	db := testutil.SetupTestDB(t)
	cleanup := testutil.ReplaceDBForTest(t, db)
	defer cleanup()

	_, _, _, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))

	ingestor := NewIngestor("test", 1, []string{"pdf"}, nil)
	ingestor.SetMemoryMessageService(servicepkg.NewMemoryMessageService(servicepkg.NewMemoryService(dao.DB, engine.Get())))
	ingestor.runMemoryTask = func(_ context.Context, _, _ string) (servicepkg.MemoryTaskDisposition, error) {
		panic("simulated memory extraction panic")
	}
	ingestor.runDocumentTask = func(ctx context.Context, _ *entity.IngestionTask) error {
		return nil
	}

	memHandle := &fakeTaskHandle{msg: common.TaskMessage{
		TaskID:   "mem-panic-2",
		TaskType: common.TaskTypeMemory,
	}}

	ingestor.handleAndExecute(memHandle)

	docHandle := &fakeTaskHandle{msg: common.TaskMessage{TaskID: taskID, TaskType: common.TaskTypeIngestionTask}}
	ingestor.handleAndExecute(docHandle)

	if memHandle.nacks.Load() != 0 || memHandle.acks.Load() != 0 {
		t.Fatalf("poison memory task: expected unsettled handle, got acks=%d nacks=%d", memHandle.acks.Load(), memHandle.nacks.Load())
	}
	if docHandle.acks.Load() != 1 || docHandle.nacks.Load() != 0 {
		t.Fatalf("document task after memory panic: expected 1 Ack/0 Nack (worker survived), got acks=%d nacks=%d", docHandle.acks.Load(), docHandle.nacks.Load())
	}
}