Skip to content

Fix ingestor Start() sync.Once re-entrant deadlock - #17600

Closed
yuzhichang wants to merge 1 commit into
infiniflow:mainfrom
yuzhichang:fix/ingestor-start-deadlock
Closed

yuzhichang wants to merge 1 commit into
infiniflow:mainfrom
yuzhichang:fix/ingestor-start-deadlock

Conversation

@yuzhichang

Copy link
Copy Markdown
Member

Start() wrapped start() in startOnce while startWorkerPool nested the same Once, deadlocking Start forever. Give worker pool its own Once and add a full-path regression test.

Start() wraps start() in e.startOnce.Do, but start() called
startWorkerPool() which nested the same startOnce. sync.Once.Do
blocks forever when re-entered from inside its own callback, so
Start() hung after InitConsumer succeeded: no worker pool, no
consumeLoop, and ingestion tasks were never consumed (stuck in
CREATED). Give startWorkerPool its own workerOnce.

Add TestStart_FullPathReturnsAndStartsWorkers which drives the real
Start() path against an embedded NATS server and asserts Start()
returns within a deadline with workers up; it fails (10s timeout)
without the fix.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds message-queue engine injection, separates worker-pool startup synchronization from overall startup, and adds a regression test for the complete ingestor startup path.

Changes

Ingestor startup lifecycle

Layer / File(s) Summary
Message-queue engine injection
internal/engine/global.go
Adds exported SetMessageQueueEngine to replace the global message-queue engine.
Worker startup synchronization and regression coverage
internal/ingestion/service/ingestion_service.go, internal/ingestion/service/ingestor_lifecycle_test.go
Adds workerOnce for worker-pool initialization and keeps startOnce for overall startup. The regression test configures an embedded NATS engine, verifies that Start() returns within 10 seconds, confirms an active worker, and stops the ingestor during cleanup.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: jinhai-cn, xugangqiang

Poem

A rabbit starts the worker train,
No locked loop remains to chain.
NATS hums and workers wake,
Start returns for progress’ sake.
Ears up high, the tests now run.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: fixing a re-entrant sync.Once deadlock in ingestor Start().
Description check ✅ Passed The description explains the deadlock, the separate Once guard, and the added regression test, but it omits the required Summary heading.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/ingestion/service/ingestor_lifecycle_test.go`:
- Around line 236-238: Update the startup assertion in the relevant lifecycle
test to poll activeWorkers until it equals the configured concurrency, using a
deadline and the same waiting pattern as
TestStartWorkerPool_StartOnceIdempotent. Replace the immediate > 0 check while
preserving failure reporting when all workers do not become active before the
deadline.
- Line 216: Update the test setup around SetMessageQueueEngine to capture the
existing engine.GetMessageQueueEngine() value before replacing it. Register a
t.Cleanup callback that restores the saved message-queue engine after the test,
while preserving the current embedded NATS shutdown behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4229b329-a9f8-406e-99c8-17831999a02b

📥 Commits

Reviewing files that changed from the base of the PR and between d69ace8 and cc2d1b2.

📒 Files selected for processing (3)
  • internal/engine/global.go
  • internal/ingestion/service/ingestion_service.go
  • internal/ingestion/service/ingestor_lifecycle_test.go

// against an embedded NATS server and asserts Start() returns within a deadline
// and that workers are actually up.
func TestStart_FullPathReturnsAndStartsWorkers(t *testing.T) {
engine.SetMessageQueueEngine(testutil.SetupNatsEngine(t))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== file locations =="
git ls-files | rg '(^|/)ingestor_lifecycle_test\.go$|testutil|engine' || true

echo "== target lines =="
if [ -f internal/ingestion/service/ingestor_lifecycle_test.go ]; then
  nl -ba internal/ingestion/service/ingestor_lifecycle_test.go | sed -n '180,235p'
fi

echo "== SetMessageQueueEngine definitions and usages =="
rg -n "func .*SetMessageQueueEngine|GetMessageQueueEngine|SetupNatsEngine" -S .

echo "== candidate engine/testutil files =="
git ls-files | rg 'testutil|engine|engine_.*\.' | sed -n '1,120p'

Repository: infiniflow/ragflow

Length of output: 2497


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== inspect likely helper/engine implementation files =="
for f in $(git ls-files | rg '(^internal/ingestion/.*/testutil|SetMessageQueueEngine|engine)\.go$'); do
  echo "--- $f"
  wc -l "$f"
  ast-grep outline "$f" --match 'SetupNatsEngine' --view expanded || true
  ast-grep outline "$f" --match 'SetMessageQueueEngine' --view expanded || true
  rg -n "func (SetupNatsEngine|SetMessageQueueEngine|GetMessageQueueEngine)|messageQueue|MessageQueue" "$f" || true
done

Repository: infiniflow/ragflow

Length of output: 1685


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== target test section =="
sed -n '180,235p' internal/ingestion/service/ingestor_lifecycle_test.go 2>/dev/null || awk '{printf "%5d\t%s\n", NR, $0}' internal/ingestion/service/ingestor_lifecycle_test.go | sed -n '180,235p'

echo "== ingestion service engine refs =="
rg -n "func .*SetMessageQueueEngine|func .*GetMessageQueueEngine|messageQueue|MessageQueue|SetupNatsEngine" internal/ingestion -S

echo "== relevant helper sections =="
for f in internal/ingestion/service/*.go internal/ingestion/testutil/*.go; do
  if rg -q "func .*(SetMessageQueueEngine|GetMessageQueueEngine|SetupNatsEngine)|messageQueue|MessageQueue" "$f"; then
    echo "--- $f"
    sed -n '1,220p' "$f"
  fi
done

Repository: infiniflow/ragflow

Length of output: 20922


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== internal/engine global.go =="
sed -n '1,220p' internal/engine/global.go

echo "== all Set/Get MessageQueueEngine definitions =="
rg -n "SetMessageQueueEngine|GetMessageQueueEngine|messageQueue" internal/engine internal -S

echo "== tests that call SetMessageQueueEngine =="
rg -n "\.SetMessageQueueEngine\(" internal -S

Repository: infiniflow/ragflow

Length of output: 6542


Restore the message-queue engine in cleanup.

SetMessageQueueEngine writes package-global engine.messageQueueEngine; SetupNatsEngine only shuts down the embedded NATS server. Save the previous value and restore it in t.Cleanup so later tests do not see a stopped engine through engine.GetMessageQueueEngine().

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/ingestion/service/ingestor_lifecycle_test.go` at line 216, Update
the test setup around SetMessageQueueEngine to capture the existing
engine.GetMessageQueueEngine() value before replacing it. Register a t.Cleanup
callback that restores the saved message-queue engine after the test, while
preserving the current embedded NATS shutdown behavior.

Comment on lines +236 to +238
if got := ing.activeWorkers.Load(); got <= 0 {
t.Fatalf("expected activeWorkers > 0 after Start(), got %d", got)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Wait for all workers before asserting startup.

Start() launches worker goroutines and returns before they necessarily enter workerLoop. An immediate read can observe activeWorkers == 0. The > 0 check also allows one of the two configured workers to fail.

Poll until activeWorkers == concurrency with a deadline, as TestStartWorkerPool_StartOnceIdempotent already does.

Proposed test adjustment
-	if got := ing.activeWorkers.Load(); got <= 0 {
-		t.Fatalf("expected activeWorkers > 0 after Start(), got %d", got)
-	}
+	deadline := time.Now().Add(2 * time.Second)
+	for time.Now().Before(deadline) && ing.activeWorkers.Load() != concurrency {
+		time.Sleep(time.Millisecond)
+	}
+	if got := ing.activeWorkers.Load(); got != concurrency {
+		t.Fatalf("expected %d active workers after Start(), got %d", concurrency, got)
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if got := ing.activeWorkers.Load(); got <= 0 {
t.Fatalf("expected activeWorkers > 0 after Start(), got %d", got)
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) && ing.activeWorkers.Load() != concurrency {
time.Sleep(time.Millisecond)
}
if got := ing.activeWorkers.Load(); got != concurrency {
t.Fatalf("expected %d active workers after Start(), got %d", concurrency, got)
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/ingestion/service/ingestor_lifecycle_test.go` around lines 236 -
238, Update the startup assertion in the relevant lifecycle test to poll
activeWorkers until it equals the configured concurrency, using a deadline and
the same waiting pattern as TestStartWorkerPool_StartOnceIdempotent. Replace the
immediate > 0 check while preserving failure reporting when all workers do not
become active before the deadline.

@yuzhichang

Copy link
Copy Markdown
Member Author

Superseded by #17599, which now bundles both #17536 regressions (MySQL Error 1101 + ingestor Start deadlock).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant