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
Add stream_wrapper() function for reliable session cleanup
Update chat_completion_handler(), embeddings(), and proxy() to use stream_wrapper instead of BackgroundTask
Remove unused BackgroundTask import
ollama.py
Add stream_wrapper() function for reliable session cleanup
Update send_post_request() to use stream_wrapper instead of BackgroundTask
Initialize session = None to prevent potential UnboundLocalError
Add streaming variable to track actual streaming state (previously used stream parameter incorrectly)
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>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/20889
Author: @Classic298
Created: 1/23/2026
Status: ❌ Closed
Base:
dev← Head:aiohttp📝 Commits (1)
cd515e6fix: 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
ollama.py
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.