From 1dea80d5be64115ab7bf3d296ae9934b692aa195 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:48:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=88=AA=E5=9B=BE=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20atexit=20=E4=BC=98=E9=9B=85=E5=85=B3?= =?UTF-8?q?=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ScreenshotService 在进程退出时通过 atexit 钩子停止后台线程, 避免 daemon 线程被强杀导致的资源泄漏。 Co-Authored-By: Claude Opus 4.5 --- operations/screenshot_service.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/operations/screenshot_service.py b/operations/screenshot_service.py index b0c3272..264154a 100644 --- a/operations/screenshot_service.py +++ b/operations/screenshot_service.py @@ -8,6 +8,7 @@ from __future__ import annotations +import atexit import os import threading import time @@ -336,3 +337,15 @@ def get_optimal_workers() -> int: if cpu_count >= 2: return 3 return 2 + + +def _shutdown_screenshot_service() -> None: + """atexit 注册的清理函数,确保进程退出时优雅关闭截图服务""" + try: + if ScreenshotService._instance is not None: + ScreenshotService._instance.stop() + except Exception: + pass + + +atexit.register(_shutdown_screenshot_service)