Skip to content

fix(api): POST /documents/stop removes partial chunks and resets counters - #15789

Merged
wangq8 merged 3 commits into
infiniflow:mainfrom
kiannidev:fix/15788-documents-stop-partial-chunks
Jun 11, 2026
Merged

wangq8 merged 3 commits into
infiniflow:mainfrom
kiannidev:fix/15788-documents-stop-partial-chunks

Conversation

@kiannidev

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

POST /api/v1/datasets/{dataset_id}/documents/stop (stop_parse_documents) cancels parsing tasks and sets run to CANCEL, but it does not remove chunks already indexed in the doc store or reset progress / chunk_num. REST callers can end up with a “cancelled” document that still returns partial chunks in GET .../chunks and in retrieval.

Legacy DELETE /api/v1/datasets/{dataset_id}/chunks (stop_parsing) already performs full cleanup: it resets counters and calls docStoreConn.delete. This PR aligns the newer stop endpoint with that behavior so both paths leave the dataset consistent.

Fixes #15788.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

Changes

  • Update stop_parse_documents in document_api.py to reset progress and chunk_num to 0 and delete partial chunks via docStoreConn.delete after cancel_all_task_of.
  • Add unit test test_stop_parse_documents_cleans_partial_chunks to assert counters reset and doc store delete is invoked.

Test plan

  • Unit test: pytest test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py::TestDocRoutesUnit::test_stop_parse_documents_cleans_partial_chunks -v
  • Manual: upload a slow document, start parse, call POST .../documents/stop while RUNNING, verify GET .../chunks returns zero chunks and UI chunk_count is 0
  • Control: legacy DELETE .../chunks behavior unchanged

Align stop_parse_documents with legacy DELETE /chunks so cancelled
documents reset progress/chunk_num and remove indexed chunks from the
doc store. Fixes infiniflow#15788.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jun 8, 2026
@coderabbitai

coderabbitai Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c3f68f79-3334-41af-8995-39766065b683

📥 Commits

Reviewing files that changed from the base of the PR and between 1453b67 and fdba599.

📒 Files selected for processing (2)
  • api/apps/restful_apis/document_api.py
  • test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • api/apps/restful_apis/document_api.py
  • test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py

📝 Walkthrough

Walkthrough

The PR updates POST /documents/stop to set run=CANCEL, reset progress and chunk_num to 0, and conditionally delete partial chunks from the tenant-scoped doc store index; a new unit test verifies both delete and missing-index behaviors.

Changes

Document parsing cancellation cleanup

Layer / File(s) Summary
Stop-parse document cleanup
api/apps/restful_apis/document_api.py, test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py
stop_parse_documents now sets run to CANCEL, resets progress and chunk_num to 0, and conditionally calls settings.docStoreConn.delete with the tenant index and dataset id to remove partial chunks. A new unit test test_stop_parse_documents_cleans_partial_chunks verifies state reset and both the docstore-delete and no-delete (index missing) paths.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • infiniflow/ragflow#14351: Updates document parsing cancellation logic to reset counters and remove doc-store records, related to migrated ingestion cancellation behavior.

Suggested labels

size:L, lgtm

Poem

🐰
I hopped through docs with tiny paws,
Found partial chunks and fixed their flaws.
Progress to zero, chunks swept away,
A tidy dataset greets the day. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: the POST /documents/stop endpoint now removes partial chunks and resets progress/chunk_num counters, which is the core fix.
Description check ✅ Passed The description includes all required template sections with clear problem statement, correct bug-fix type selection, specific changes listed, and test plan provided.
Linked Issues check ✅ Passed The PR fully addresses all coding requirements from issue #15788: resets progress/chunk_num to 0, deletes partial chunks via docStoreConn.delete, adds comprehensive unit test, and aligns the newer endpoint with legacy behavior.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: the document_api.py fix to stop_parse_documents and the corresponding unit test are both necessary for the bug fix with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@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: 1

🧹 Nitpick comments (1)
api/apps/restful_apis/document_api.py (1)

1627-1637: ⚡ Quick win

Add logging for the new stop+cleanup flow.

The new cleanup path cancels tasks, resets counters, and deletes chunks, but emits no success/failure log, which makes incident tracing harder.

As per coding guidelines, **/*.py: Add logging for new flows.

🤖 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 `@api/apps/restful_apis/document_api.py` around lines 1627 - 1637, Add
structured logging around the stop+cleanup flow: log an info-level success
message after cancel_all_task_of(doc_id), DocumentService.update_by_id(...) and
settings.docStoreConn.delete(...) complete (include doc_id, tenant_id,
dataset_id and updated run/progress/chunk_num values), and add an error-level
log if settings.docStoreConn.delete throws (catch the exception, log
doc_id/tenant_id/dataset_id and the exception details, and ensure success_count
is only incremented on successful completion). Use the existing symbols
cancel_all_task_of, DocumentService.update_by_id, settings.docStoreConn.delete
and success_count to locate where to add these logs.

Source: Coding guidelines

🤖 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 `@api/apps/restful_apis/document_api.py`:
- Line 1636: Wrap the call to settings.docStoreConn.delete that uses
search.index_name(tenant_id), dataset_id and doc.id in a safe guard so it cannot
raise a 500 if the index/table is missing: either check for the index/table
existence first or catch the specific "not found" / missing-index exception
around settings.docStoreConn.delete and swallow/ log it (do not rethrow),
ensuring the document state changes already performed (the CANCEL state and
zeroed counters) remain in place; use the delete call site
(settings.docStoreConn.delete(... search.index_name(tenant_id) ...)) and handle
only the error variants that indicate a missing index rather than all
exceptions.

---

Nitpick comments:
In `@api/apps/restful_apis/document_api.py`:
- Around line 1627-1637: Add structured logging around the stop+cleanup flow:
log an info-level success message after cancel_all_task_of(doc_id),
DocumentService.update_by_id(...) and settings.docStoreConn.delete(...) complete
(include doc_id, tenant_id, dataset_id and updated run/progress/chunk_num
values), and add an error-level log if settings.docStoreConn.delete throws
(catch the exception, log doc_id/tenant_id/dataset_id and the exception details,
and ensure success_count is only incremented on successful completion). Use the
existing symbols cancel_all_task_of, DocumentService.update_by_id,
settings.docStoreConn.delete and success_count to locate where to add these
logs.
🪄 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

Run ID: 73d7b499-47ef-413b-a71c-00a30a71c71a

📥 Commits

Reviewing files that changed from the base of the PR and between f2aadd3 and 1453b67.

📒 Files selected for processing (2)
  • api/apps/restful_apis/document_api.py
  • test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py

Comment thread api/apps/restful_apis/document_api.py Resolved
Check index_exist before deleting partial chunks so cancellation does
not return 500 when the doc store index is missing. Fixes infiniflow#15788.
@kiannidev

Copy link
Copy Markdown
Contributor Author

Hi, @wangq8 , @Haruko386 , @JinHai-CN @yingfeng @xugangqiang @buua436
Please review this PR

@wangq8 wangq8 added the ci Continue Integration label Jun 11, 2026
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (a1dc2da) to head (be533d2).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #15789   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wangq8
wangq8 merged commit de18313 into infiniflow:main Jun 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: POST /documents/stop leaves partial chunks indexed and does not reset chunk counts

2 participants