Repository navigation
Fix ingestor Start() sync.Once re-entrant deadlock - #17600
yuzhichang wants to merge 1 commit into
Conversation
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.
📝 WalkthroughWalkthroughThe 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. ChangesIngestor startup lifecycle
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
internal/engine/global.gointernal/ingestion/service/ingestion_service.gointernal/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)) |
There was a problem hiding this comment.
🎯 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
doneRepository: 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
doneRepository: 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 -SRepository: 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.
| if got := ing.activeWorkers.Load(); got <= 0 { | ||
| t.Fatalf("expected activeWorkers > 0 after Start(), got %d", got) | ||
| } |
There was a problem hiding this comment.
🩺 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.
| 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.
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.