fix(api): isolate side-effect session writes in multimodal and RAG handlers#38210
Open
agarwalpranav0711 wants to merge 2 commits into
Open
fix(api): isolate side-effect session writes in multimodal and RAG handlers#38210agarwalpranav0711 wants to merge 2 commits into
agarwalpranav0711 wants to merge 2 commits into
Conversation
…ndlers Extends the session isolation fix from langgenius#37895 to two files that were missed: base_app_runner.py and index_tool_callback_handler.py. Both files used db.session.commit() on the shared Flask request-scoped session to persist side-effect writes (a MessageFile during multimodal LLM streaming, and DatasetQuery audit logs / hit_count updates during RAG retrieval). This flushed all pending ORM changes in the request transaction, not just the intended record, risking premature commits and DetachedInstanceError under concurrency. Both now use independent sessionmaker sessions matching the established pattern from langgenius#37895. Fixes langgenius#38176
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-06-30 13:45:52.202826908 +0000
+++ /tmp/pyrefly_pr.txt 2026-06-30 13:45:43.870745082 +0000
@@ -4226,17 +4226,17 @@
ERROR Object of class `NoneType` has no attribute `status` [missing-attribute]
--> tests/unit_tests/core/base/test_app_generator_tts_publisher.py:188:16
ERROR Argument `None` is not assignable to parameter `app_id` with type `str` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.__init__` [bad-argument-type]
- --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:66:20
+ --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:84:20
ERROR Argument `None` is not assignable to parameter `message_id` with type `str` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.__init__` [bad-argument-type]
- --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:67:24
+ --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:85:24
ERROR Argument `None` is not assignable to parameter `user_id` with type `str` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.__init__` [bad-argument-type]
- --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:68:21
+ --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:86:21
ERROR Argument `None` is not assignable to parameter `invoke_from` with type `InvokeFrom` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.__init__` [bad-argument-type]
- --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:69:25
+ --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:87:25
ERROR Argument `None` is not assignable to parameter `query` with type `str` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.on_query` [bad-argument-type]
- --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:72:26
+ --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:90:26
ERROR Argument `None` is not assignable to parameter `dataset_id` with type `str` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.on_query` [bad-argument-type]
- --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:72:32
+ --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:90:32
ERROR Argument `list[DummyToolInvokeMessage]` is not assignable to parameter `tool_outputs` with type `Iterable[ToolInvokeMessage]` in function `core.callback_handler.workflow_tool_callback_handler.DifyWorkflowCallbackHandler.on_tool_execution` [bad-argument-type]
--> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:55:30
ERROR Argument `list[DummyToolInvokeMessage]` is not assignable to parameter `tool_outputs` with type `Iterable[ToolInvokeMessage]` in function `core.callback_handler.workflow_tool_callback_handler.DifyWorkflowCallbackHandler.on_tool_execution` [bad-argument-type]
@@ -4581,7 +4581,7 @@
ERROR Argument `SimpleNamespace` is not assignable to parameter `tenant` with type `Tenant` in function `core.plugin.backwards_invocation.model.PluginModelBackwardsInvocation.invoke_summary` [bad-argument-type]
--> tests/unit_tests/core/plugin/test_backwards_invocation_model.py:54:72
ERROR Generator function should return `Generator` [bad-return]
- --> tests/unit_tests/core/plugin/test_model_runtime_adapter.py:72:51
+ --> tests/unit_tests/core/plugin/test_model_runtime_adapter.py:49:51
ERROR Class member `_BadString.__str__` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
--> tests/unit_tests/core/plugin/test_plugin_entities.py:238:17
ERROR Argument `TestPluginDaemonEntities.test_credential_type_helpers._FakeCredential` is not assignable to parameter `self` with type `CredentialType` in function `core.plugin.entities.plugin_daemon.CredentialType.get_name` [bad-argument-type]
|
Contributor
Pyrefly Type Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
Fixes #<issue number>.Summary
Fixes #38176
Extends the session isolation fix from #37895 to two files that were missed:
base_app_runner.pyandindex_tool_callback_handler.py.Both files used
db.session.commit()on the shared Flask request-scoped session to persist side-effect writes (aMessageFileduring multimodal LLM streaming, andDatasetQueryaudit logs /hit_countupdates during RAG retrieval). This flushed all pending ORM changes in the request transaction, not just the intended record, risking premature commits andDetachedInstanceErrorunder concurrency.Changes:
base_app_runner.py:_handle_multimodal_image_content()now persists theMessageFilethrough an independent sessionmaker session instead of the shared session.index_tool_callback_handler.py:on_query()andon_tool_end()now persist audit/analytics writes through an independent session, following the same pattern as fix(api): isolate side-effect database writes #37895.Screenshots
N/A - backend transaction/session fix
Checklist
make lint && make type-check(backend) andcd web && pnpm exec vp staged(frontend) to appease the lint gods