Skip to content

fix(rag): atomically claim tenant task queues - #46310

Closed
mikamikasuki wants to merge 6 commits into
langgenius:mainfrom
mikamikasuki:fix/tenant-isolated-queue-atomic-claim
Closed

mikamikasuki wants to merge 6 commits into
langgenius:mainfrom
mikamikasuki:fix/tenant-isolated-queue-atomic-claim

Conversation

@mikamikasuki

@mikamikasuki mikamikasuki commented Nov 13, 2026 •

Copy link
Copy Markdown

Fixes #46309

Summary

The tenant-isolated queue checked for an active task with GET and then set its marker separately. Concurrent first submissions could both see an idle queue and dispatch directly, exceeding TENANT_ISOLATED_TASK_CONCURRENCY.

Use Redis SET NX EX as an atomic queue claim in the RAG pipeline and batch document indexing proxies. Submissions that lose the claim are appended to the existing FIFO waiting queue.

A barrier-based regression test reproduces two simultaneous first submissions and verifies that only one is dispatched directly.

Screenshots

Not applicable (backend change).

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues.
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend).

Focused validation: 155 relevant tests passed; Ruff check and format, targeted Pyrefly core check, and no-new-cast/no-new-test-doubles guards passed.

From Codex

Use Redis SET NX EX when routing the first tenant-isolated task so concurrent submissions cannot both bypass the per-tenant queue.
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-13 16:49:45.396607449 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-13 16:49:30.744456001 +0000
@@ -5578,9 +5578,9 @@
 ERROR Argument `() -> tuple[StartNode, Iterator[NodeRunFailedEvent]]` is not assignable to parameter `getter` with type `() -> tuple[Node[Unknown], Generator[CustomContainerRequest | IterationFrameRequest | LoopFrameRequest | NodeEvent]]` in function `services.rag_pipeline.rag_pipeline.RagPipelineService._handle_node_run_result` [bad-argument-type]
     --> tests/unit_tests/services/rag_pipeline/test_rag_pipeline.py:1949:20
 ERROR Argument `Literal['']` is not assignable to parameter `plan` with type `CloudPlan` in function `RagPipelineTaskProxyTestDataFactory.create_mock_features` [bad-argument-type]
-   --> tests/unit_tests/services/rag_pipeline/test_rag_pipeline_task_proxy.py:579:91
+   --> tests/unit_tests/services/rag_pipeline/test_rag_pipeline_task_proxy.py:635:91
 ERROR Argument `None` is not assignable to parameter `plan` with type `CloudPlan` in function `RagPipelineTaskProxyTestDataFactory.create_mock_features` [bad-argument-type]
-   --> tests/unit_tests/services/rag_pipeline/test_rag_pipeline_task_proxy.py:604:91
+   --> tests/unit_tests/services/rag_pipeline/test_rag_pipeline_task_proxy.py:660:91
 ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (dict[str, object]) [no-matching-overload]
   --> tests/unit_tests/services/rag_pipeline/test_rag_pipeline_transform_service.py:29:18
 ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (dict[str, object]) [no-matching-overload]

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 85.45% 85.45% +0.00%
Strict coverage 85.11% 85.11% +0.00%
Typed symbols 91,162 91,171 +9
Untyped symbols 15,589 15,589 0
Modules 4522 4522 0

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.40%. Comparing base (05f3723) to head (0817086).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #46310   +/-   ##
=======================================
  Coverage   90.40%   90.40%           
=======================================
  Files        6332     6332           
  Lines      396620   396620           
  Branches    79708    79708           
=======================================
+ Hits       358548   358550    +2     
+ Misses      31962    31961    -1     
+ Partials     6110     6109    -1     
Flag Coverage Δ
api 90.09% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

SET NX EX fixes the race between concurrent first submissions. Two races remain in the queue handoff at 08170861.

  1. A queued job can stall. B fails to claim the queue. Before B calls push_tasks(), A finishes, finds the FIFO empty, and clears the marker. B then appends to the FIFO, but no worker is left to dispatch it. Submission · Worker completion

  2. An old worker can clear a newer claim. If A's marker expires while A is still running, B can claim the queue. When A finishes and finds the FIFO empty, its unconditional DEL removes B's marker. A third job can then claim the queue while B is still running. Marker operations

Could we make submission and completion atomic in the shared queue code, and check a unique ownership token before a worker dequeues, renews, or clears a claim? If the atomic operation touches both Redis keys, the marker and FIFO need a shared Redis Cluster hash tag so they land in the same slot. Changing the key format also needs a migration plan for existing queued jobs. Key definitions

@mikamikasuki

Copy link
Copy Markdown
Author

Thanks — both gaps are real on this head. The queue marker is a one-hour boolean separate from the FIFO: an enqueue can race the empty-queue handoff, and an expired worker can delete a successor's claim. I’m treating both as blockers to this fix. I’ll rework the queue handoff around atomic ownership and enqueue/completion transitions, preserve FIFO behavior, and document a safe path for existing queue keys and Redis Cluster slotting. I’ll update the PR after focused regressions cover both interleavings and the compatibility path.

@41tair 41tair closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tenant-isolated queue can dispatch concurrent first tasks beyond its per-tenant limit

3 participants