From 06c2bea806bc435f30d8b1da240c30455bf2fa8b Mon Sep 17 00:00:00 2001 From: TheSmallHanCat Date: Mon, 26 Jan 2026 20:12:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=9B=B4=E6=96=B0=E7=BC=BA=E5=A4=B1=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=8F=8A=E6=97=A5=E5=BF=97=E7=8A=B6=E6=80=81=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复update_admin_config方法未更新task_retry_enabled、task_max_retries、auto_disable_on_401字段的问题 - 新增finally块确保请求日志在异常情况下也能正确更新状态,避免卡在status_code=-1 --- src/core/database.py | 7 +++++-- src/services/generation_handler.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/core/database.py b/src/core/database.py index 1b746b6..1a3f298 100644 --- a/src/core/database.py +++ b/src/core/database.py @@ -1010,9 +1010,12 @@ class Database: async with aiosqlite.connect(self.db_path) as db: await db.execute(""" UPDATE admin_config - SET admin_username = ?, admin_password = ?, api_key = ?, error_ban_threshold = ?, updated_at = CURRENT_TIMESTAMP + SET admin_username = ?, admin_password = ?, api_key = ?, error_ban_threshold = ?, + task_retry_enabled = ?, task_max_retries = ?, auto_disable_on_401 = ?, + updated_at = CURRENT_TIMESTAMP WHERE id = 1 - """, (config.admin_username, config.admin_password, config.api_key, config.error_ban_threshold)) + """, (config.admin_username, config.admin_password, config.api_key, config.error_ban_threshold, + config.task_retry_enabled, config.task_max_retries, config.auto_disable_on_401)) await db.commit() # Proxy config operations diff --git a/src/services/generation_handler.py b/src/services/generation_handler.py index 6218f5f..ae91bc9 100644 --- a/src/services/generation_handler.py +++ b/src/services/generation_handler.py @@ -521,6 +521,7 @@ class GenerationHandler: task_id = None is_first_chunk = True # Track if this is the first chunk log_id = None # Initialize log_id + log_updated = False # Track if log has been updated try: # Create initial log entry BEFORE submitting task to upstream @@ -680,6 +681,7 @@ class GenerationHandler: status_code=200, duration=duration ) + log_updated = True # Mark log as updated except Exception as e: # Release lock for image generation on error @@ -727,6 +729,7 @@ class GenerationHandler: status_code=status_code, duration=duration ) + log_updated = True # Mark log as updated else: # Generic error await self.db.update_request_log( @@ -735,12 +738,35 @@ class GenerationHandler: status_code=500, duration=duration ) + log_updated = True # Mark log as updated # Wrap exception with token_id information if token_obj: raise GenerationError(str(e), token_id=token_obj.id) else: raise e + finally: + # Ensure log is updated even if exception handling fails + # This prevents logs from being stuck at status_code = -1 + if log_id and not log_updated: + try: + # Log was not updated in try or except blocks, update it now + duration = time.time() - start_time + await self.db.update_request_log( + log_id, + response_body=json.dumps({"error": "Task failed or interrupted during processing"}), + status_code=500, + duration=duration + ) + debug_logger.log_info(f"Updated stuck log entry {log_id} from status -1 to 500 in finally block") + except Exception as finally_error: + # Don't let finally block errors break the flow + debug_logger.log_error( + error_message=f"Failed to update log in finally block: {str(finally_error)}", + status_code=500, + response_text=str(finally_error) + ) + async def handle_generation_with_retry(self, model: str, prompt: str, image: Optional[str] = None, video: Optional[str] = None,