fix(backend): clear orphaned Redis task IDs on startup and explicit stop (#22525)
Description
This PR resolves a critical state-synchronization bug where generation tasks orphaned by a server restart would "brick" the UI in a permanent loading state. By implementing a startup cleanup routine and hardening the manual stop endpoint, we ensure Redis and in-memory state stay consistent even after crashes or rollouts.
Added
clear_all_tasks() function in backend/open_webui/tasks.py to handle bulk cleanup of Redis hash keys (REDIS_TASKS_KEY) and set patterns (REDIS_ITEM_TASKS_KEY:*).
Changed
lifespan handler in backend/open_webui/main.py now calls clear_all_tasks on startup to ensure a clean slate.
stop_task in backend/open_webui/tasks.py now explicitly deletes keys from Redis and local dictionaries instead of relying on background worker callbacks which may be dead.
Fixed
Fixed#22525: Orphaned task IDs in Redis causing permanent loading spinners in the sidebar and chat view.
Improved stop_task reliability by adding byte-decoding for Redis values and idempotent deletion logic.
Pull Request Checklist
Target branch: Verify that the pull request targets the dev branch.
Description: Concise description provided above.
Changelog: Entry added below.
Documentation: N/A (Internal backend fix).
Dependencies: No new dependencies.
Testing: Verified that orphaned tasks are cleared on startup and manual "Stop" now effectively unfreezes the UI.
Agentic AI Code: Code has undergone manual human review and syntax validation.
Git Hygiene: Atomic change; rebased on latest code.
Changelog Entry
Fixed
Resolved issue #22525 where server restarts left orphaned generation tasks in Redis, causing the UI to hang indefinitely.
Additional Information
Issue handled: When the server restarts, background workers die before cleaning up their task IDs. The /stop endpoint previously only sent an abort signal; if no worker was alive to receive it, the Redis key stayed forever. This fix adds a "Hard Delete" to the stop command and a "Flush on Boot" to the server.
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.
## 📋 Pull Request Information
**Original PR:** https://github.com/open-webui/open-webui/pull/23044
**Author:** [@snesan821](https://github.com/snesan821)
**Created:** 3/25/2026
**Status:** 🔄 Open
**Base:** `dev` ← **Head:** `main`
---
### 📝 Commits (1)
- [`c092420`](https://github.com/open-webui/open-webui/commit/c0924206d775b8837247759032c0fb730f73cb46) fix(backend): clear orphaned Redis task IDs on startup and explicit stop (#22525)
### 📊 Changes
**2 files changed** (+56 additions, -0 deletions)
<details>
<summary>View changed files</summary>
📝 `backend/open_webui/main.py` (+4 -0)
📝 `backend/open_webui/tasks.py` (+52 -0)
</details>
### 📄 Description
# fix(backend): clear orphaned Redis task IDs on startup and explicit stop (#22525)
### Description
This PR resolves a critical state-synchronization bug where generation tasks orphaned by a server restart would "brick" the UI in a permanent loading state. By implementing a startup cleanup routine and hardening the manual stop endpoint, we ensure Redis and in-memory state stay consistent even after crashes or rollouts.
### Added
- `clear_all_tasks()` function in `backend/open_webui/tasks.py` to handle bulk cleanup of Redis hash keys (`REDIS_TASKS_KEY`) and set patterns (`REDIS_ITEM_TASKS_KEY:*`).
### Changed
- `lifespan` handler in `backend/open_webui/main.py` now calls `clear_all_tasks` on startup to ensure a clean slate.
- `stop_task` in `backend/open_webui/tasks.py` now explicitly deletes keys from Redis and local dictionaries instead of relying on background worker callbacks which may be dead.
### Fixed
- Fixed #22525: Orphaned task IDs in Redis causing permanent loading spinners in the sidebar and chat view.
- Improved `stop_task` reliability by adding byte-decoding for Redis values and idempotent deletion logic.
---
# Pull Request Checklist
- [x] **Target branch:** Verify that the pull request targets the `dev` branch.
- [x] **Description:** Concise description provided above.
- [x] **Changelog:** Entry added below.
- [x] **Documentation:** N/A (Internal backend fix).
- [x] **Dependencies:** No new dependencies.
- [x] **Testing:** Verified that orphaned tasks are cleared on startup and manual "Stop" now effectively unfreezes the UI.
- [x] **Agentic AI Code:** Code has undergone manual human review and syntax validation.
- [x] **Code review:** Self-review performed; Ruff linting passed.
- [x] **Git Hygiene:** Atomic change; rebased on latest code.
# Changelog Entry
### Fixed
- Resolved issue #22525 where server restarts left orphaned generation tasks in Redis, causing the UI to hang indefinitely.
---
### Additional Information
- **Issue handled:** When the server restarts, background workers die before cleaning up their task IDs. The `/stop` endpoint previously only sent an abort signal; if no worker was alive to receive it, the Redis key stayed forever. This fix adds a "Hard Delete" to the stop command and a "Flush on Boot" to the server.
### Contributor License Agreement
- [x] 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.
---
<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/23044
Author: @snesan821
Created: 3/25/2026
Status: 🔄 Open
Base:
dev← Head:main📝 Commits (1)
c092420fix(backend): clear orphaned Redis task IDs on startup and explicit stop (#22525)📊 Changes
2 files changed (+56 additions, -0 deletions)
View changed files
📝
backend/open_webui/main.py(+4 -0)📝
backend/open_webui/tasks.py(+52 -0)📄 Description
fix(backend): clear orphaned Redis task IDs on startup and explicit stop (#22525)
Description
This PR resolves a critical state-synchronization bug where generation tasks orphaned by a server restart would "brick" the UI in a permanent loading state. By implementing a startup cleanup routine and hardening the manual stop endpoint, we ensure Redis and in-memory state stay consistent even after crashes or rollouts.
Added
clear_all_tasks()function inbackend/open_webui/tasks.pyto handle bulk cleanup of Redis hash keys (REDIS_TASKS_KEY) and set patterns (REDIS_ITEM_TASKS_KEY:*).Changed
lifespanhandler inbackend/open_webui/main.pynow callsclear_all_taskson startup to ensure a clean slate.stop_taskinbackend/open_webui/tasks.pynow explicitly deletes keys from Redis and local dictionaries instead of relying on background worker callbacks which may be dead.Fixed
stop_taskreliability by adding byte-decoding for Redis values and idempotent deletion logic.Pull Request Checklist
devbranch.Changelog Entry
Fixed
Additional Information
/stopendpoint previously only sent an abort signal; if no worker was alive to receive it, the Redis key stayed forever. This fix adds a "Hard Delete" to the stop command and a "Flush on Boot" to the server.Contributor License Agreement
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.