mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-07 03:18:23 -05:00
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/16016
Author: @Godzilla675
Created: 7/25/2025
Status: ❌ Closed
Base:
main← Head:main📝 Commits (8)
9b8e8adInitial plan5b49c1eInitial plan79a0458Fix unclosed aiohttp client sessions when stopping tool tasks58f2c7fFix unclosed client session errors during tool execution cancellationf3287a8Merge pull request #1 from Godzilla675/copilot/fix-1342ccd3-cd80-4505-ae44-1bf76a649439adc5086Revert "Fix unclosed client session errors during tool execution cancellation"fe1c3a2Merge pull request #4 from Godzilla675/revert-1-copilot/fix-1342ccd3-cd80-4505-ae44-1bf76a6494396ff571fMerge pull request #3 from Godzilla675/copilot/fix-f7ce3b69-3e20-4a60-8c49-0c4395d3ca09📊 Changes
1 file changed (+27 additions, -3 deletions)
View changed files
📝
backend/open_webui/utils/tools.py(+27 -3)📄 Description
Before submitting, make sure you've checked the following:
Target branch: Please verify that the pull request targets the dev branch.
Description: Provide a concise description of the changes made in this pull request.
Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
Testing: Have you written and run sufficient tests to validate the changes?
Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following: fix (Bug fix or error correction)
Problem
When users clicked the stop button during tool execution in chat completions, the backend would log "Unclosed client session" and "Unclosed connector" warnings. This issue only occurred when tools were involved in the generation process.
Root Cause
The problem was in the execute_tool_server() and get_tool_server_data() functions in /backend/open_webui/utils/tools.py. These functions used the async with aiohttp.ClientSession() pattern, but when tasks containing these functions were cancelled via the stop button, the session cleanup might not complete properly during cancellation.
Solution
Modified both functions to ensure proper session cleanup even when tasks are cancelled by implementing manual session management with explicit cleanup in try/finally blocks and dedicated asyncio.CancelledError handlers.
Changelog Entry
Description
Fixed unclosed aiohttp client sessions when stopping tool tasks, eliminating resource leaks and "Unclosed client session" warnings that occurred when users cancelled tool execution mid-process.
Added
Explicit asyncio.CancelledError handling in tool server functions
Informative logging when tool operations are cancelled
Changed
Modified execute_tool_server() to use manual session management with defensive cleanup
Modified get_tool_server_data() to use manual session management with defensive cleanup
Replaced async with aiohttp.ClientSession() pattern with explicit session creation and cleanup
Fixed
Resource leak: Unclosed aiohttp client sessions when tasks are cancelled
Warning messages: "Unclosed client session" and "Unclosed connector" errors in logs
Session cleanup: Proper session closure during task cancellation scenarios
Additional Information
This fix ensures HTTP sessions are properly closed in all scenarios: normal completion, error conditions, and task cancellation
No breaking changes - all existing tool execution behavior is preserved
Affects only the backend tool execution infrastructure
Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.