[PR #20889] [CLOSED] fix: fix aiohttp session leak in streaming responses #48860

Closed
opened 2026-04-30 01:02:50 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/20889
Author: @Classic298
Created: 1/23/2026
Status: Closed

Base: devHead: aiohttp


📝 Commits (1)

  • cd515e6 fix: fix aiohttp session leak in ollama router (#144)

📊 Changes

2 files changed (+32 additions, -19 deletions)

View changed files

📝 backend/open_webui/routers/ollama.py (+17 -6)
📝 backend/open_webui/routers/openai.py (+15 -13)

📄 Description

Summary

Fixes "Unclosed client session" errors that occur when aiohttp sessions are not properly closed during streaming responses.

Root cause: BackgroundTask cleanup is unreliable - it doesn't execute when clients disconnect during streaming, causing session leaks that accumulate over time.

Solution: Replace BackgroundTask with a stream_wrapper async generator that guarantees cleanup in its finally block, which executes even when streaming is interrupted.

Changes

openai.py

  1. Add stream_wrapper() function for reliable session cleanup
  2. Update chat_completion_handler(), embeddings(), and proxy() to use stream_wrapper instead of BackgroundTask
  3. Remove unused BackgroundTask import

ollama.py

  1. Add stream_wrapper() function for reliable session cleanup
  2. Update send_post_request() to use stream_wrapper instead of BackgroundTask
  3. Initialize session = None to prevent potential UnboundLocalError
  4. Add streaming variable to track actual streaming state (previously used stream parameter incorrectly)
  5. Remove unused BackgroundTask import

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.

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/20889 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 1/23/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `aiohttp` --- ### 📝 Commits (1) - [`cd515e6`](https://github.com/open-webui/open-webui/commit/cd515e6dd53d26362335a4b20405077d57f767cb) fix: fix aiohttp session leak in ollama router (#144) ### 📊 Changes **2 files changed** (+32 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/routers/ollama.py` (+17 -6) 📝 `backend/open_webui/routers/openai.py` (+15 -13) </details> ### 📄 Description # Summary Fixes "Unclosed client session" errors that occur when aiohttp sessions are not properly closed during streaming responses. Root cause: BackgroundTask cleanup is unreliable - it doesn't execute when clients disconnect during streaming, causing session leaks that accumulate over time. Solution: Replace BackgroundTask with a stream_wrapper async generator that guarantees cleanup in its finally block, which executes even when streaming is interrupted. ### Changes **openai.py** 1. Add stream_wrapper() function for reliable session cleanup 2. Update chat_completion_handler(), embeddings(), and proxy() to use stream_wrapper instead of BackgroundTask 3. Remove unused BackgroundTask import **ollama.py** 1. Add stream_wrapper() function for reliable session cleanup 2. Update send_post_request() to use stream_wrapper instead of BackgroundTask 3. Initialize session = None to prevent potential UnboundLocalError 4. Add streaming variable to track actual streaming state (previously used stream parameter incorrectly) 5. Remove unused BackgroundTask import ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-30 01:02:50 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#48860