修复所有资源泄漏问题(P0级bug)

修复的Bug:
- Bug #21: Playwright浏览器实例泄漏
- Bug #22: 数据库连接泄漏(已由连接池解决)
- Bug #23: 截图文件句柄泄漏
- Bug #24: 线程资源未清理
- Bug #25: requests.Session对象泄漏

主要改进:
1. PlaywrightAutomation类:
   - 添加atexit注册,确保进程退出时关闭浏览器
   - 添加__enter__/__exit__支持context manager
   - 添加_closed标志防止重复关闭
   - 添加_cleanup_on_exit静默清理方法

2. APIBrowser类:
   - 添加atexit注册,确保Session正确关闭
   - 添加__enter__/__exit__支持context manager
   - 添加_closed标志防止重复关闭

3. 截图功能增强:
   - 使用临时文件机制
   - 添加文件大小验证
   - 失败时自动清理临时文件
   - 确保不产生垃圾文件

4. 应用关闭清理:
   - 添加cleanup_on_exit()函数
   - 注册SIGINT/SIGTERM信号处理器
   - 停止所有运行中的任务
   - 等待线程优雅退出
   - 关闭浏览器线程池
   - 关闭数据库连接池

影响:
- 防止长期运行导致的内存泄漏
- 确保进程异常退出时正确清理资源
- 提升系统稳定性和可靠性

受影响文件:
- playwright_automation.py
- api_browser.py
- app.py

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-11 13:48:06 +08:00
parent 795ff7f1a7
commit 114a4107bb
3 changed files with 156 additions and 2 deletions

60
app.py
View File

@@ -3484,7 +3484,67 @@ def batch_stop_accounts():
"stopped": stopped
})
def cleanup_on_exit():
"""应用退出时的清理函数"""
import signal
import sys
print("\n正在清理资源...")
# 1. 停止所有运行中的任务
print("- 停止运行中的任务...")
for account_id, thread in list(active_tasks.items()):
try:
# 设置停止标志
if account_id in user_accounts:
for user_id in user_accounts:
if account_id in user_accounts[user_id]:
user_accounts[user_id][account_id].should_stop = True
except:
pass
# 2. 等待所有线程完成最多等待5秒
print("- 等待线程退出...")
for account_id, thread in list(active_tasks.items()):
try:
if thread and thread.is_alive():
thread.join(timeout=2)
except:
pass
# 3. 关闭浏览器工作线程池
print("- 关闭浏览器线程池...")
try:
shutdown_browser_worker_pool()
except:
pass
# 4. 关闭数据库连接池
print("- 关闭数据库连接池...")
try:
db_pool._pool.close_all() if db_pool._pool else None
except:
pass
print("✓ 资源清理完成")
if __name__ == '__main__':
import signal
import atexit
# 注册退出清理函数
atexit.register(cleanup_on_exit)
# 注册信号处理器
def signal_handler(sig, frame):
print("\n\n收到退出信号,正在关闭...")
cleanup_on_exit()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
print("=" * 60)
print("知识管理平台自动化工具 - 多用户版")
print("=" * 60)