Skip to content

fix(ingestor): dedup memory-task redelivery to prevent duplicate extraction - #8

Closed
xugangqiang wants to merge 1 commit into
fix/ingestion-backpressurefrom
fix/memory-task-redelivery-dedup
Closed

xugangqiang wants to merge 1 commit into
fix/ingestion-backpressurefrom
fix/memory-task-redelivery-dedup

Conversation

@xugangqiang

@xugangqiang xugangqiang commented Aug 17, 2026 •

Copy link
Copy Markdown
Owner

Problem

Memory-extraction tasks share the tasks.RAGFLOW NATS consumer and the ingestor worker pool with ingestion tasks, but they do not use the ingestion state machine (no ingestion_task row). processMessage's memory branch therefore lacked the claimTask dedup guard that the ingestion branch has.

Under a NATS redelivery — an AckWait timeout while a slow LLM extraction is running, or an ingestor restart — a redelivered memory message was dispatched to the worker again, because nothing rejected the second transition. This double-executed the extraction:

  • a second extractByLLM call (extra LLM token cost), and
  • a second embedAndSaveMessages → InsertChunks, producing duplicate extracted memory entries in the store.

The blocking send introduced by the backpressure fix (PR infiniflow#18369) made this race reachable: a redelivery arriving while the first run is still in flight has no guard and is scheduled a second time.

Correct behavior

A redelivered memory task that is already being processed must be ack-skipped, never re-executed. The in-flight claim must be released when the worker settles the task (so a later genuine redelivery can retry) and on shutdown (so the broker redelivers after restart).

Fix

In processMessage's memory branch:

  • take a claimTask(taskID) guard before enqueuing; on conflict ack-skip the duplicate;
  • release the claim in the ctx.Done() (shutdown) path so the broker redelivers the task after restart instead of blocking the consume loop;
  • release the claim in executeMemoryTask via defer e.releaseTask(taskID) once the worker settles, mirroring executeTask.

This makes the memory branch symmetric with the ingestion branch's dedup handling.

Tests

  • TestProcessMessage_DuplicateMemoryDeliveryAckSkips: delivers the same memory TaskID twice (worker pool not started, so the claim is held in the channel) and asserts the second delivery is ack-skipped with no second enqueue.
  • TestProcessMessage_DuplicateIngestionDeliveryAckSkips: asserts the same guarantee for the ingestion branch (injected duplicate delivery → ack-skip, exactly one execution, exactly one COMPLETED row).

Dependency

Stacks on infiniflow#18369 (ingestion backpressure fix), which converted the memory branch to a blocking send and established the in-flight claimTask/releaseTask pattern this PR reuses. Merge infiniflow#18369 first.

processMessage now applies the same claim-based dedup guard to memory
tasks that it already applies to ingestion tasks. A redelivered memory
message (same TaskID) is ack-skipped instead of being dispatched to the
worker a second time, which previously duplicated the extraction. The
claim is released by executeMemoryTask on settle, mirroring executeTask.

Tests:
- TestProcessMessage_DuplicateIngestionDeliveryAckSkips drives the
  redelivery/claim path the burst test cannot reach (its simulated broker
  only redelivers on Nack, and the fixed code never Nacks). It injects a
  second delivery of the same taskID while the first is in flight and
  asserts the duplicate is ack-skipped and the task executes exactly once.
- TestProcessMessage_DuplicateMemoryDeliveryAckSkips verifies the new
  memory-branch dedup claim.
@xugangqiang

Copy link
Copy Markdown
Owner Author

Superseded by the upstream PR on infiniflow/ragflow (head xugangqiang:fix/memory-task-redelivery-dedup, base main).

xugangqiang added a commit that referenced this pull request Oct 30, 2026
- prefer worker-claim time (doc.ProcessBeginAt) for wait duration (#2)
- omit zero-value times from the JSON log line instead of emitting
  0001-01-01, and guard the logger with atomic.Pointer for a safe swap (#8, #7)
- populate DurationMS from ParseDurationS so consumers need not recompute (#11)
- drop the unused IngestLog.Code field (#Nit)
- thread the failure reason through MarkFailed into the emitted event's
  ErrorInfo, so failed runs are no longer logged with an empty reason (#4)
- persist process_end_at idempotently (WHERE process_end_at IS NULL) so
  overlapping terminal events cannot rewrite the recorded completion
  instant (#12)

All changes covered by unit tests in internal/common, internal/observability
and internal/service.
xugangqiang added a commit that referenced this pull request Nov 2, 2026
- prefer worker-claim time (doc.ProcessBeginAt) for wait duration (#2)
- omit zero-value times from the JSON log line instead of emitting
  0001-01-01, and guard the logger with atomic.Pointer for a safe swap (#8, #7)
- populate DurationMS from ParseDurationS so consumers need not recompute (#11)
- drop the unused IngestLog.Code field (#Nit)
- thread the failure reason through MarkFailed into the emitted event's
  ErrorInfo, so failed runs are no longer logged with an empty reason (#4)
- persist process_end_at idempotently (WHERE process_end_at IS NULL) so
  overlapping terminal events cannot rewrite the recorded completion
  instant (#12)
- emit the running lifecycle event from PrepareValidatedRun AFTER the
  worker-claim time is stamped, so BeginTime and wait_duration_s reflect
  this run instead of a zero or previous-run value (#A)
- only emit user_name when set, so the always-empty key is not logged and
  the JSON field uses omitempty (#C)
- correct the ingestor.log sink docs: it always writes to stdout;
  ingestor.log.path only adds an optional rotated file on top (#B)
- drop the redundant SetIngestLog/SyncIngestLog calls from admin and
  syncer, which never emit ingestion events (#D)

All changes covered by unit tests in internal/common, internal/observability
and internal/service.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant