fix: 截图服务添加 atexit 优雅关闭

ScreenshotService 在进程退出时通过 atexit 钩子停止后台线程,
避免 daemon 线程被强杀导致的资源泄漏。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.5 committed 2026-05-20 22:48:43 +08:00
1 parent 5ce314b41c
commit 1dea80d5be
1 file changed
+13
+13
View File
@@ -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)