Skip to content

fix: 修复 3 个高危运行时缺陷(检测循环静默死亡 / 直连下载失败误报成功 / 退出截断转码产物) - #521

Open
zzuu080603 wants to merge 3 commits into
ihmily:mainfrom
zzuu080603:fix/p1-runtime-stability
Open

zzuu080603 wants to merge 3 commits into
ihmily:mainfrom
zzuu080603:fix/p1-runtime-stability

Conversation

@zzuu080603

Copy link
Copy Markdown

📜 标题(Title)

  • 修复代码审查发现的 3 个高危运行时缺陷:无效保存目录导致周期检测任务静默死亡、直连下载失败被误报为“录制完成”、退出时 os._exit(0) 硬杀后台转码任务留下截断的 mp4。

🔍 描述(Description)

三个缺陷相互独立,均为确定性可复现,各自对应一个独立 commit:

1. 无效保存目录杀死周期检测循环(7a893e9)
periodic_check 的 while True 中 await self.check_free_space() 无异常保护。当 live_save_path 指向不存在的目录(网络盘掉线/路径笔误/磁盘未挂载)时,shutil.disk_usage 抛 FileNotFoundError,整个检测任务静默死亡——此后开播检测、磁盘恢复检测全部停摆,UI 无任何提示,录制功能“看似正常但永不触发”。

  • 循环体包 try/except,单轮失败仅记日志并在下个间隔重试
  • check_free_space 硬化:OSError 时回退默认录制目录重查,两处都失败则跳过本轮(不向上抛)

2. 直连下载失败伪装成录制完成(4c5db59)
_download_stream 收到非 2xx(403/404/断链)时直接 return,无异常无标志;外层 start_direct_download 只看 download_task.done() 即走 _handle_recording_finished,日志打出 "Direct Downloading Completed"。失败的拉流对用户呈现为正常结束,还可能触发“直播结束”推送。

  • DirectStreamDownloader 新增 failed 标志:非 2xx 与 I/O 异常置位,手动取消保持干净
  • 外层按 failed and not should_stop 分流至 _handle_recording_error(RECORDING_ERROR 状态 + 错误提示)

3. 退出时后台转码被硬杀(19820ba)
关闭应用时 converts_mp4/custom_script_execute 将任务移交 BackgroundService(非 daemon 线程),但两条退出路径在有限等待后调用 os._exit(0)——它不等待任何线程(包括非 daemon),队列中的转码/脚本任务被拦腰杀死,留下截断的 mp4。

  • 新增 _wait_background_service():os._exit 前轮询排空队列,上限 120s,每 10s 打进度日志
  • 超时后明示放弃(error 日志 + 剩余任务数),把“静默截断”变成“有界、可观测的放弃”
  • 同时覆盖开播推送任务(同样经 BackgroundService)

📝 类型(Type of Change)

  • 修复Bug
  • 新功能
  • 代码风格更新(格式化,局部变量)
  • 重构(改进代码结构)
  • 构建相关更改(依赖项,构建脚本等)
  • 其他:_ _

🏗️ 测试(Testing)

独立 venv(按 requirements.txt 安装依赖 + imageio-ffmpeg 提供真实 ffmpeg)导入生产代码冒烟,2/2 通过:

  • 无效目录:live_save_path 指向不存在路径 + 2s 轮询间隔,日志按间隔出现 5 条 Cannot inspect disk space(循环存活且持续重试),9s 内完成 5 次迭代,recording_enabled 经回退目录保持为 True(修复前第一条异常后循环即死)
  • 退出排空:真实链路 converts_mp4(recording_enabled=False 模拟关闭)→ BackgroundService → ffmpeg remux 2s ts → _wait_background_service():mp4 产物 Duration 2.04s、双流完整、原件移入 original/(converts_success=True 的判据);负例(30s 慢任务 + 3s 预算)精确 3.0s 返回并打出放弃日志
  • py_compile 与 ruff check(4 个改动文件)均通过

📋 检查清单(Checklist)

  • 我已经阅读贡献指南文档(仓库无独立 CONTRIBUTING.md,按 PR 模板执行)
  • 我的更改没有产生新的警告(ruff 通过)
  • 我已经添加了覆盖我更改的测试(headless 冒烟 2 项 + 负例;未引入单元测试框架,与仓库现状一致)
  • 我已经相应地更新了文档(如果适用)(纯缺陷修复,无新增配置项,默认行为不变)
  • 我遵循了这个项目的代码风格

三个缺陷来自代码审查(无对应 issue);修复均保持向后兼容,默认行为不变。

qinzihan added 3 commits November 10, 2026 17:41
…invalid

shutil.disk_usage raises FileNotFoundError when the configured live_save_path
does not exist (network drive offline, typo, unmounted disk), which killed the
periodic_check task silently - live detection stopped with no UI hint.

- wrap the periodic_check loop body so one failing iteration only logs and
  retries on the next interval
- harden check_free_space: on OSError inspect the default recordings dir as a
  fallback, and skip the check (instead of raising) when both fail
… success

A non-2xx response (403/404/dead link) made _download_stream return cleanly,
so start_direct_download reported 'Direct Downloading Completed' and the task
went back to MONITORING - a failed pull looked like a finished recording and
could even fire a stream-end push.

- DirectStreamDownloader gains a failed flag, set on non-2xx responses and on
  I/O errors (manual cancel stays clean)
- start_direct_download routes failed pulls to _handle_recording_error
  (RECORDING_ERROR status + snack) instead of _handle_recording_finished
converts_mp4 and custom_script_execute hand their work to BackgroundService
(a non-daemon thread) when the app is closing, but both exit paths called
os._exit(0) after a bounded wait, which kills the process without running
any thread - queued transcodes died mid-write and left truncated mp4 files.

- add _wait_background_service(): poll the queue for up to 120s (progress log
  every 10s) before os._exit, covering both the with-recordings and the
  direct exit paths
- past the deadline the remaining tasks are abandoned explicitly with an
  error log instead of dying silently
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant