Skip to content

fix: guard docStoreConn.delete with index_exist in parse and stop_parsing - #15876

Merged
wangq8 merged 3 commits into
infiniflow:mainfrom
jason020818:fix/guard-docstore-delete-index-missing
Jun 11, 2026
Merged

wangq8 merged 3 commits into
infiniflow:mainfrom
jason020818:fix/guard-docstore-delete-index-missing

Conversation

@jason020818

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Closes #15874

Both the POST /api/v1/datasets/<dataset_id>/chunks (re-parse) and DELETE /api/v1/datasets/<dataset_id>/chunks (stop-parsing) handlers called settings.docStoreConn.delete unconditionally. When the tenant/dataset index has not been created yet — fresh dataset, first parse interrupted before any chunks were indexed, or index manually removed — the delete call throws and the handler returns HTTP 500 after the document state was already mutated (RUNNING with zeroed counters for the parse path; CANCEL with zeroed counters for the stop path), leaving the document in an inconsistent state.

The newer parse_documents path in document_api.py already uses index_exist before deleting:

How to fix?

Apply the same index_exist guard to both call sites in chunk_api.py:

  • parse (POST path, line ~192): guard the delete before TaskService.filter_delete.
  • stop_parsing (DELETE path, line ~242): guard the delete after DocumentService.update_by_id.

Both sites already have the correct search.index_name(tenant_id) and dataset_id parameters; the guard is a one-line addition at each site.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

…sing

Both the POST /chunks (parse) and DELETE /chunks (stop_parsing) paths
called settings.docStoreConn.delete unconditionally. When the tenant/dataset
index has not been created yet (fresh dataset, first parse interrupted before
any chunks were indexed, or index manually removed), the delete throws and
the handler returns HTTP 500 after the document state was already mutated to
RUNNING (parse path) or CANCEL with zeroed counters (stop path).

The newer parse_documents path in document_api.py already uses index_exist
before deleting:

    if settings.docStoreConn.index_exist(search.index_name(tenant_id), doc.kb_id):
        settings.docStoreConn.delete(...)

Apply the same guard to both sites in chunk_api.py.

Closes infiniflow#15874
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jun 9, 2026
@coderabbitai

coderabbitai Bot commented Jun 9, 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: 4368d98f-1d5b-4f6a-96f2-4b5e1b506ee5

📥 Commits

Reviewing files that changed from the base of the PR and between 378c986 and 50af4d9.

📒 Files selected for processing (1)
  • api/apps/restful_apis/chunk_api.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • api/apps/restful_apis/chunk_api.py

📝 Walkthrough

Walkthrough

The PR guards two chunk datastore delete calls in chunk_api.py with index_exist(...) checks. Both the legacy parse and stop_parsing flows now verify the tenant/dataset search index exists before calling settings.docStoreConn.delete(...), and log when deletion is skipped.

Changes

Chunk Datastore Deletion Guards

Layer / File(s) Summary
Conditional document deletion guards
api/apps/restful_apis/chunk_api.py
In both parse and stop_parsing, document deletion from the chunk datastore is now guarded by index_exist() checks. Deletion is only attempted if the search index exists for the tenant/dataset, matching the pattern in document_api.parse_documents and preventing errors when the index is missing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related issues

  • #15874: [Bug] Legacy DELETE /chunks stop can return HTTP 500 when doc store index is missing — This PR fixes the issue by adding index_exist guards around unconditional delete calls in stop_parsing and legacy parse.

Poem

🐰 I hop by the code gate at night,
I check for indexes before I bite.
If none are found, I skip the prune,
Keeping runs tidy by the light of the moon. 🌙

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 pull request title clearly and concisely describes the main change: adding index existence guards to docStoreConn.delete calls in two specific functions.
Description check ✅ Passed The pull request description fully addresses the required template sections: problem statement with linked issue reference, clear explanation of the fix, and correct classification as a bug fix.
Linked Issues check ✅ Passed The PR addresses all objectives from #15874: guards are added at both call sites (parse and stop_parsing), the index_exist pattern matches the document_api.py implementation, and the fix prevents HTTP 500 errors when the index is missing.
Out of Scope Changes check ✅ Passed All changes are focused and in-scope: modifications are limited to the two required functions in chunk_api.py with conditional index checks before deletions, matching the stated objective.

✏️ 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.

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

192-193: ⚡ Quick win

Add logging for the new “skip delete when index is missing” path.

Both guarded branches silently skip cleanup. Add an info/warning log so operators can trace why chunk deletion was not attempted for a document during parse/stop flows.

Proposed patch
-        if settings.docStoreConn.index_exist(search.index_name(tenant_id), dataset_id):
-            settings.docStoreConn.delete({"doc_id": id}, search.index_name(tenant_id), dataset_id)
+        if settings.docStoreConn.index_exist(search.index_name(tenant_id), dataset_id):
+            settings.docStoreConn.delete({"doc_id": id}, search.index_name(tenant_id), dataset_id)
+        else:
+            logging.info(
+                "Skip docStore delete in parse: index missing. tenant_id=%s dataset_id=%s doc_id=%s",
+                tenant_id,
+                dataset_id,
+                id,
+            )
@@
-        if settings.docStoreConn.index_exist(search.index_name(tenant_id), dataset_id):
-            settings.docStoreConn.delete({"doc_id": doc[0].id}, search.index_name(tenant_id), dataset_id)
+        if settings.docStoreConn.index_exist(search.index_name(tenant_id), dataset_id):
+            settings.docStoreConn.delete({"doc_id": doc[0].id}, search.index_name(tenant_id), dataset_id)
+        else:
+            logging.info(
+                "Skip docStore delete in stop_parsing: index missing. tenant_id=%s dataset_id=%s doc_id=%s",
+                tenant_id,
+                dataset_id,
+                doc[0].id,
+            )

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

Also applies to: 243-244

🤖 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/chunk_api.py` around lines 192 - 193, The guarded
deletion currently silently skips when the index is missing; add a log entry
(logger.info or logger.warning) immediately inside the else/skip path where
settings.docStoreConn.index_exist(search.index_name(tenant_id), dataset_id) is
false so operators can trace why settings.docStoreConn.delete({"doc_id": id},
search.index_name(tenant_id), dataset_id) was not attempted; include clear
context (tenant_id, dataset_id, doc_id/id, and the resolved
search.index_name(tenant_id)) and mirror the same logging change at the other
guarded branch later in the file where delete is conditionally skipped so both
parse/stop flows are observable.

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.

Nitpick comments:
In `@api/apps/restful_apis/chunk_api.py`:
- Around line 192-193: The guarded deletion currently silently skips when the
index is missing; add a log entry (logger.info or logger.warning) immediately
inside the else/skip path where
settings.docStoreConn.index_exist(search.index_name(tenant_id), dataset_id) is
false so operators can trace why settings.docStoreConn.delete({"doc_id": id},
search.index_name(tenant_id), dataset_id) was not attempted; include clear
context (tenant_id, dataset_id, doc_id/id, and the resolved
search.index_name(tenant_id)) and mirror the same logging change at the other
guarded branch later in the file where delete is conditionally skipped so both
parse/stop flows are observable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 81f3d827-1c8f-4ec5-993a-88ff1f542b17

📥 Commits

Reviewing files that changed from the base of the PR and between 87b8062 and f5a6f0b.

📒 Files selected for processing (1)
  • api/apps/restful_apis/chunk_api.py

Address CodeRabbit review on infiniflow#15876 by emitting info logs in parse and
stop_parsing when docStoreConn.index_exist returns false.

Co-authored-by: Cursor <[email protected]>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jun 9, 2026
@jason020818

Copy link
Copy Markdown
Contributor Author

Addressed CodeRabbit review feedback in the latest push (logging, non-dict JSON guard, consecutive header merge + tests). Ready for another look.

1 similar comment
@jason020818

Copy link
Copy Markdown
Contributor Author

Addressed CodeRabbit review feedback in the latest push (logging, non-dict JSON guard, consecutive header merge + tests). Ready for another look.

@jason020818

Copy link
Copy Markdown
Contributor Author

@wangq8 @kiannidev Small XS bugfix for #15874 — guards docStoreConn.delete with index_exist (same pattern as document_api.py). Latest push adds skip logging per review. Ready for review.

@jason020818

Copy link
Copy Markdown
Contributor Author

@wangq8 Friendly follow-up — this XS bugfix has been open ~2 days, mergeable_state: clean, and guards docStoreConn.delete with index_exist (same pattern as document upload). Closes #15874. Happy to address any feedback. Thanks!

@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 (47fb462) to head (50af4d9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #15876   +/-   ##
=======================================
  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.

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]: Legacy DELETE /chunks stop can return HTTP 500 when doc store index is missing

2 participants