[PR #26480] fix: document is not bound to a session #31447

Closed
opened 2026-02-21 20:49:29 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/26480

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #26473
Fixes #26511
Fixes #26525

Fix DetachedInstanceError in KnowledgeIndexNode by Caching Scalars Before Commits

Summary

  • Prevent SQLAlchemy DetachedInstanceError when finalizing knowledge indexing by avoiding ORM attribute access after downstream commits.

Context

  • During indexing, index_processor.index(...) invokes DatasetDocumentStore.add_documents(...), which commits inside a loop. With SQLAlchemy’s default expire_on_commit=True, ORM attributes are expired at each commit. Subsequent attribute access (e.g., document.id) triggers a refresh on a possibly detached instance and raises DetachedInstanceError.

Root Cause

  • After index_processor.index(...) returns, the node reads document.id / dataset.id to aggregate/update rows and build the return payload. Because downstream commits expired attributes (and the instance may be detached), these reads trigger a refresh that fails.

Key References

  • Downstream commits: api/core/rag/docstore/dataset_docstore.py:172 (commit inside add loop)
  • Index entry point: api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:161 (calls index_processor.index)
  • Post-index reads (previously problematic):
    • Word count aggregation: api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:167
    • Segment status update: api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:177
    • Return payload attributes: api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:190

What Changed

  • Cache required scalar values before invoking the indexer, then use the cached values afterwards to avoid attribute refresh:
    • Cached at api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:139:
      • doc_id_value (document.id)
      • ds_id_value (dataset.id)
      • dataset_name_value (dataset.name)
      • document_name_value (document.name)
      • created_at_value (document.created_at)
    • Use cached IDs in queries/updates at api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:167 and :177.
    • Build return payload from cached values at api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:190.
    • Set display_status to literal "completed" in the payload to avoid reading an expired attribute.

Screenshots

image

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. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/26480 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary Fixes #26473 Fixes #26511 Fixes #26525 Fix DetachedInstanceError in KnowledgeIndexNode by Caching Scalars Before Commits Summary - Prevent SQLAlchemy DetachedInstanceError when finalizing knowledge indexing by avoiding ORM attribute access after downstream commits. Context - During indexing, `index_processor.index(...)` invokes `DatasetDocumentStore.add_documents(...)`, which commits inside a loop. With SQLAlchemy’s default `expire_on_commit=True`, ORM attributes are expired at each commit. Subsequent attribute access (e.g., `document.id`) triggers a refresh on a possibly detached instance and raises `DetachedInstanceError`. Root Cause - After `index_processor.index(...)` returns, the node reads `document.id` / `dataset.id` to aggregate/update rows and build the return payload. Because downstream commits expired attributes (and the instance may be detached), these reads trigger a refresh that fails. Key References - Downstream commits: `api/core/rag/docstore/dataset_docstore.py:172` (commit inside add loop) - Index entry point: `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:161` (calls `index_processor.index`) - Post-index reads (previously problematic): - Word count aggregation: `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:167` - Segment status update: `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:177` - Return payload attributes: `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:190` What Changed - Cache required scalar values before invoking the indexer, then use the cached values afterwards to avoid attribute refresh: - Cached at `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:139`: - `doc_id_value` (document.id) - `ds_id_value` (dataset.id) - `dataset_name_value` (dataset.name) - `document_name_value` (document.name) - `created_at_value` (document.created_at) - Use cached IDs in queries/updates at `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:167` and `:177`. - Build return payload from cached values at `api/core/workflow/nodes/knowledge_index/knowledge_index_node.py:190`. - Set `display_status` to literal `"completed"` in the payload to avoid reading an expired attribute. <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> ## Screenshots <img width="403" height="406" alt="image" src="https://github.com/user-attachments/assets/0392cb7f-797f-4dba-9484-45580a0d723b" /> ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:49:29 -05:00
yindo closed this issue 2026-02-21 20:49:29 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31447