[PR #23710] [CLOSED] fix(db): patch SQLAlchemy ↔ aiosqlite ≥0.20 terminate_force_close incompatibility #50377

Closed
opened 2026-04-30 03:04:27 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/23710
Author: @Classic298
Created: 4/14/2026
Status: Closed

Base: devHead: claude/fix/aiosqlite-terminate-shim


📝 Commits (3)

  • 6bda95d fix(db): patch SQLAlchemy ↔ aiosqlite ≥0.20 terminate_force_close incompatibility
  • a03bde4 fix(db): tighten aiosqlite terminate-shim install scope and worker wake-up
  • 4c45ab6 fix(db): aiosqlite shim — bytecode fallback when source inspection fails

📊 Changes

2 files changed (+211 additions, -0 deletions)

View changed files

backend/open_webui/internal/_aiosqlite_compat.py (+198 -0)
📝 backend/open_webui/internal/db.py (+13 -0)

📄 Description

SQLAlchemy 2.0.36+ added a terminate_force_close() path on its async DB-API adapters, used by the connection pool when a connection must be invalidated immediately (typically when an in-flight query was cancelled — request aborted by client, timeout, etc.).

For the aiosqlite dialect, _terminate_force_close unconditionally accesses self._connection.stop. That attribute existed on aiosqlite.Connection up to 0.18 (where Connection sub-classed threading.Thread) and was removed in 0.20+ when the worker model was refactored.

On the version pin Open WebUI ships (sqlalchemy 2.0.48 + aiosqlite 0.21.0) every cancelled aiosqlite call therefore produces a multi-page

NotImplementedError: terminate_force_close() not implemented by this DBAPI shim

ERROR traceback, drowning real errors in noise even though the underlying connection is correctly torn down by aiosqlite's own worker on the next loop tick.

Add internal/_aiosqlite_compat.install() which monkey-patches the shim to understand both APIs:

  • aiosqlite ≤ 0.18 — call Connection.stop() as before.
  • aiosqlite ≥ 0.20 — flip the worker thread's _running flag so it exits on the next iteration (the same end-state the original code achieved on the old API).
  • Anything else — log at debug and fall back to GC.

The patch is idempotent, runs once at db.py import time before the async engine is created, and is a no-op on installations whose SQLAlchemy doesn't ship the affected symbol — so it won't break future upstream rewrites.

Contributor License Agreement

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/23710 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 4/14/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `claude/fix/aiosqlite-terminate-shim` --- ### 📝 Commits (3) - [`6bda95d`](https://github.com/open-webui/open-webui/commit/6bda95d3bd46ebb88898689ea416e09a61960b03) fix(db): patch SQLAlchemy ↔ aiosqlite ≥0.20 terminate_force_close incompatibility - [`a03bde4`](https://github.com/open-webui/open-webui/commit/a03bde4701193a3e5c5598c5cd2ae2d12fb26c77) fix(db): tighten aiosqlite terminate-shim install scope and worker wake-up - [`4c45ab6`](https://github.com/open-webui/open-webui/commit/4c45ab659a477a6817dad94b9aab37216c4b33ad) fix(db): aiosqlite shim — bytecode fallback when source inspection fails ### 📊 Changes **2 files changed** (+211 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `backend/open_webui/internal/_aiosqlite_compat.py` (+198 -0) 📝 `backend/open_webui/internal/db.py` (+13 -0) </details> ### 📄 Description SQLAlchemy 2.0.36+ added a `terminate_force_close()` path on its async DB-API adapters, used by the connection pool when a connection must be invalidated immediately (typically when an in-flight query was cancelled — request aborted by client, timeout, etc.). For the aiosqlite dialect, `_terminate_force_close` unconditionally accesses `self._connection.stop`. That attribute existed on `aiosqlite.Connection` up to 0.18 (where Connection sub-classed `threading.Thread`) and was removed in 0.20+ when the worker model was refactored. On the version pin Open WebUI ships (sqlalchemy 2.0.48 + aiosqlite 0.21.0) every cancelled aiosqlite call therefore produces a multi-page NotImplementedError: terminate_force_close() not implemented by this DBAPI shim ERROR traceback, drowning real errors in noise even though the underlying connection is correctly torn down by aiosqlite's own worker on the next loop tick. Add `internal/_aiosqlite_compat.install()` which monkey-patches the shim to understand both APIs: * aiosqlite ≤ 0.18 — call `Connection.stop()` as before. * aiosqlite ≥ 0.20 — flip the worker thread's `_running` flag so it exits on the next iteration (the same end-state the original code achieved on the old API). * Anything else — log at debug and fall back to GC. The patch is idempotent, runs once at db.py import time before the async engine is created, and is a no-op on installations whose SQLAlchemy doesn't ship the affected symbol — so it won't break future upstream rewrites. ### Contributor License Agreement <!-- 🚨 DO NOT DELETE THE TEXT BELOW 🚨 Keep the "Contributor License Agreement" confirmation text intact. Deleting it will trigger the CLA-Bot to INVALIDATE your PR. Your PR will NOT be reviewed or merged until you check the box below confirming that you have read and agree to the terms of the CLA. --> - [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. > [!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 03:04:27 -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#50377