From 315ff579f30f3d82feb4856642a89518a63e9f33 Mon Sep 17 00:00:00 2001 From: ifer47 <361301399@qq.com> Date: Wed, 17 Jun 2026 01:51:46 +0800 Subject: [PATCH] fix: close SQLAlchemy sessions in datasource_provider_service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pattern `with Session(db.engine).no_autoflush as session:` creates a Session that is never closed. The `no_autoflush` context manager only manages the autoflush flag — it does NOT close the session when the `with` block exits. This leaks database connections from the connection pool. Both `is_system_oauth_params_exist()` and `get_oauth_client()` are read-only SELECT queries that don't need `no_autoflush` at all. Replace with the standard `with Session(db.engine) as session:` pattern used elsewhere in the codebase, which properly closes the session on exit. Co-Authored-By: zhipu/glm-5 --- api/services/datasource_provider_service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/services/datasource_provider_service.py b/api/services/datasource_provider_service.py index 12807a41f04dd0..cd796d2d7670a0 100644 --- a/api/services/datasource_provider_service.py +++ b/api/services/datasource_provider_service.py @@ -433,7 +433,7 @@ def is_system_oauth_params_exist(self, datasource_provider_id: DatasourceProvide """ check if system oauth params exist """ - with Session(db.engine).no_autoflush as session: + with Session(db.engine) as session: return ( session.scalar( select(DatasourceOauthParamConfig) @@ -510,7 +510,7 @@ def get_oauth_client(self, tenant_id: str, datasource_provider_id: DatasourcePro """ provider = datasource_provider_id.provider_name plugin_id = datasource_provider_id.plugin_id - with Session(db.engine).no_autoflush as session: + with Session(db.engine) as session: # get tenant oauth client params tenant_oauth_client_params = session.scalar( select(DatasourceOauthTenantParamConfig)