Skip to content

fix(tools): use short-lived sessions for icon lookups to prevent idle-in-transaction - #36903

Merged
fatelei merged 2 commits into
langgenius:mainfrom
goingforstudying-ctrl:fix/36902-idle-in-transaction
Jun 2, 2026
Merged

fatelei merged 2 commits into
langgenius:mainfrom
goingforstudying-ctrl:fix/36902-idle-in-transaction

Conversation

@goingforstudying-ctrl

Copy link
Copy Markdown
Contributor

Fixes #36902

Replace db.session with explicit Session(db.engine) context managers in generate_workflow_tool_icon_url() and generate_api_tool_icon_url().

This prevents DB transactions from remaining open during long-running nested workflow execution, which causes PostgreSQL connections to stay 'idle in transaction' for minutes.

The MCP tool icon URL already uses this pattern; this brings workflow and API tools into alignment.

…-in-transaction

Replace db.session (long-lived Flask SQLAlchemy session bound to request
context) with explicit Session(db.engine) context managers in:
- generate_workflow_tool_icon_url()
- generate_api_tool_icon_url()

This prevents the database transaction from remaining open while a nested
Workflow Tool or API Tool executes long-running HTTP/LLM calls, which
causes PostgreSQL connections to stay 'idle in transaction' for minutes
until idle_in_transaction_session_timeout kills them.

The MCP tool icon URL already uses the same pattern; this change brings
workflow and API tools into alignment.

Fixes langgenius#36902
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 1, 2026
@github-actions

github-actions Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-06-02 01:41:47.105793986 +0000
+++ /tmp/pyrefly_pr.txt	2026-06-02 01:41:34.142700929 +0000
@@ -67,7 +67,7 @@
 ERROR Class member `PluginToolProviderController.entity` overrides parent class `BuiltinToolProviderController` in an inconsistent manner [bad-override-mutable-attribute]
   --> core/tools/plugin_tool/provider.py:12:5
 ERROR Cannot set item in `dict[str, dict[str, Any]]` [unsupported-operation]
-    --> core/tools/tool_manager.py:1110:58
+    --> core/tools/tool_manager.py:1117:58
 ERROR `(method: str, url: str, max_retries: int = ..., **kwargs: Any) -> httpx._models.Response` is not assignable to attribute `perform_request` with type `(self: CloudScraper, method: Unknown, url: Unknown, *args: Unknown, **kwargs: Unknown) -> requests.models.Response` [bad-assignment]
   --> core/tools/utils/web_reader_tool.py:66:35
 ERROR `list[Never]` is not assignable to attribute `tools` with type `Never` [bad-assignment]
@@ -5340,11 +5340,11 @@
 ERROR Argument `Literal['']` is not assignable to parameter `typ` with type `Literal['api', 'builtin', 'mcp', 'workflow'] | None` in function `core.tools.tool_manager.ToolManager.list_providers_from_api` [bad-argument-type]
    --> tests/unit_tests/core/tools/test_tool_manager.py:678:97
 ERROR `SimpleNamespace` is not assignable to attribute `entity` with type `ToolProviderEntityWithPlugin` [bad-assignment]
-   --> tests/unit_tests/core/tools/test_tool_manager.py:820:30
+   --> tests/unit_tests/core/tools/test_tool_manager.py:839:30
 ERROR `SimpleNamespace` is not assignable to attribute `entity` with type `ToolProviderEntityWithPlugin` [bad-assignment]
-   --> tests/unit_tests/core/tools/test_tool_manager.py:844:30
+   --> tests/unit_tests/core/tools/test_tool_manager.py:863:30
 ERROR Cannot index into `str` [bad-index]
-   --> tests/unit_tests/core/tools/test_tool_manager.py:849:20
+   --> tests/unit_tests/core/tools/test_tool_manager.py:868:20
 ERROR Argument `SimpleNamespace` is not assignable to parameter `controller` with type `ToolProviderController` in function `core.tools.utils.encryption.create_tool_provider_encrypter` [bad-argument-type]
    --> tests/unit_tests/core/tools/utils/test_encryption.py:203:75
 ERROR `None` is not subscriptable [unsupported-operation]

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 45.98% 45.98% 0.00%
Strict coverage 45.49% 45.49% 0.00%
Typed symbols 24,871 24,871 0
Untyped symbols 29,534 29,534 0
Modules 2770 2770 0

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 2, 2026
@fatelei
fatelei added this pull request to the merge queue Jun 2, 2026
Merged via the queue into langgenius:main with commit c64d3e9 Jun 2, 2026
31 checks passed
@goingforstudying-ctrl
goingforstudying-ctrl deleted the fix/36902-idle-in-transaction branch June 7, 2026 10:04
Ssazurov pushed a commit to Ssazurov/dify that referenced this pull request Aug 8, 2026
…-in-transaction (langgenius#36903)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

idle in transaction when Workflow Tool runs: generate_workflow_tool_icon_url keeps db.session open during long nested workflow execution

2 participants