From 4a170254671a67eeb5c7a9f1bc51881901a55dda Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 16 Sep 2025 18:01:44 +0800 Subject: [PATCH] fix(trigger): update session management in TriggerProviderService - Changed session management in `TriggerProviderService` from `autoflush=True` to `expire_on_commit=False` for improved control over session state. - This change enhances the reliability of database interactions by preventing automatic expiration of objects after commit, ensuring data consistency during trigger operations. These updates contribute to better session handling and stability in trigger-related functionalities. --- api/services/trigger/trigger_provider_service.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api/services/trigger/trigger_provider_service.py b/api/services/trigger/trigger_provider_service.py index 4a24db0db7..7cd794a636 100644 --- a/api/services/trigger/trigger_provider_service.py +++ b/api/services/trigger/trigger_provider_service.py @@ -60,7 +60,7 @@ class TriggerProviderService: """List all trigger subscriptions for the current tenant""" subscriptions: list[TriggerProviderSubscriptionApiEntity] = [] workflows_in_use_map: dict[str, int] = {} - with Session(db.engine, autoflush=False) as session: + with Session(db.engine, expire_on_commit=False) as session: # Get all subscriptions subscriptions_db = ( session.query(TriggerSubscription) @@ -128,7 +128,7 @@ class TriggerProviderService: """ try: provider_controller = TriggerManager.get_trigger_provider(tenant_id, provider_id) - with Session(db.engine, autoflush=False) as session: + with Session(db.engine, expire_on_commit=False) as session: # Use distributed lock to prevent race conditions lock_key = f"trigger_provider_create_lock:{tenant_id}_{provider_id}" with redis_client.lock(lock_key, timeout=20): @@ -317,7 +317,7 @@ class TriggerProviderService: :return: OAuth client configuration or None """ provider_controller = TriggerManager.get_trigger_provider(tenant_id, provider_id) - with Session(db.engine, autoflush=False) as session: + with Session(db.engine, expire_on_commit=False) as session: tenant_client: TriggerOAuthTenantClient | None = ( session.query(TriggerOAuthTenantClient) .filter_by( @@ -485,7 +485,7 @@ class TriggerProviderService: :param provider_id: Provider identifier :return: True if enabled, False otherwise """ - with Session(db.engine, autoflush=False) as session: + with Session(db.engine, expire_on_commit=False) as session: custom_client = ( session.query(TriggerOAuthTenantClient) .filter_by( @@ -503,7 +503,7 @@ class TriggerProviderService: """ Get a trigger subscription by the endpoint ID. """ - with Session(db.engine, autoflush=False) as session: + with Session(db.engine, expire_on_commit=False) as session: subscription = session.query(TriggerSubscription).filter_by(endpoint_id=endpoint_id).first() if not subscription: return None