Skip to content

fix: 修复 6 个可确定性复现的可靠性缺陷(事件循环死锁 / 卡死状态机 / 配置丢失 / 假通知) - #518

Open
zzuu080603 wants to merge 5 commits into
ihmily:mainfrom
zzuu080603:fix/reliability-hardening
Open

zzuu080603 wants to merge 5 commits into
ihmily:mainfrom
zzuu080603:fix/reliability-hardening

Conversation

@zzuu080603

Copy link
Copy Markdown

📜 标题(Title)

  • 修复 6 个可确定性复现的可靠性缺陷:事件循环死锁、录制器注册泄漏、假录制状态、用户配置静默丢失、Telegram 假成功通知、直连下载失败被当作完成。

🔍 描述(Description)

1. recordings 锁导致事件循环死锁
add_recording/remove_recording/clear_all_recordings 是协程,持 threading.Lock 期间 await 写盘。此时另一录制操作尝试获取锁会阻塞整个事件循环线程,持锁方永远无法恢复释放锁 → 整个应用永久冻结(UI 无响应、监控停止、只能杀进程)。已用最小复现验证:同一段逻辑,旧写法确定性死锁(连 asyncio 的超时机制都失效),asyncio.Lock 正常完成。批量删除房间时写盘窗口内再点任何房间操作即可触发。

2. 录制器注册泄漏 → 房间永久跳过检测
start_ffmpeg/start_direct_download 的异常路径(如 ffmpeg 不在 PATH、spawn 失败)从不注销 active_recorders 中的条目,而 check_if_live 一进门就跳过已注册的房间——唯一能删除/替换条目的路径恰恰要经过被跳过的检测,形成闭环,只能重启恢复。

3. start_recording 前置段异常 → 假录制(#471 同族)
start_update() 已置 is_recording=True 后,start_recording 的准备段(文件名/输出目录/makedirs)不在任何 try 内;该协程经 fire-and-forget 调度,异常被完全吞掉 → 卡片永远"录制中"但无文件,监控永久跳过。现在异常会复位标志、置错误状态并注销。

4. 损坏的用户配置被默认配置静默覆盖
user_settings.json 为空或 JSON 损坏(磁盘故障/网盘同步冲突/手动编辑出错)时,init_user_config 直接用默认模板整体覆盖,所有个性化设置不可逆丢失。现在先备份到 user_settings.json.bak(带警告日志)再重建。

5. Telegram 推送失败被无条件上报为成功
_async_post 吞掉所有异常返回 {'error': ...},且 Telegram API 以 HTTP 200 + {'ok': false} 报告拒绝——代码从不检查,send_to_telegram 恒返回成功,"Push Telegram message successfully" 误导排障。现在要求 {'ok': True} 才算成功,失败时带 description 上报。

6. 直连下载失败被当作"录制完成"
DirectStreamDownloader 吞掉全部失败(链接过期 403/DNS/代理),start_direct_download 用 task.done() 判断结束,无法区分成败 → 空文件/残缺文件触发"下载完成"推送。现在下载器记录 self.error,调用方在失败时置 RECORDING_ERROR、不发完成通知,并经 recheck_live_status 自动重试;手动停止行为不变。

另外 remove_active_recorder 增加身份校验(is self),防止迟退出的旧录制器误删已替换它的新录制器注册。

📝 类型(Type of Change)

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

🏗️ 测试(Testing)

对每项修复编写了行为级验证脚本(加载项目真实源码,stub 第三方依赖),红→绿 双向验证:

  • 修复后代码:21/21 全部通过(锁类型与 25 并发增删无死锁、异常路径 AST+行为验证注销、身份校验、损坏/空 JSON 备份与重建、正常配置不受影响、Telegram ok=False/网络异常→error、HTTP 403/DNS 异常记录 error、前置段异常后 is_recording 复位/状态复位/注册清理)
  • 修复前代码:8 项确定性 FAIL;并发锁测试在旧代码上直接死锁挂起整个测试进程(事件循环被阻塞后一切超时机制失效),已单独用最小复现演示(threading.Lock 死锁 / asyncio.Lock 正常)
  • ruff check 与语法编译全部通过

复现方式(均为确定性,无需真实直播平台):并发调用带锁方法、让 create_subprocess_exec 抛异常、输出目录指向不可创建路径、写入损坏 JSON、mock Telegram 响应 {'ok': false}、mock 下载 HTTP 403。

📋 检查清单(Checklist)

  • 我已经阅读贡献指南文档(仓库未提供独立 CONTRIBUTING.md,已按 .github/PULL_REQUEST_TEMPLATE.md 的要求与本检查清单执行)
  • 我的更改没有产生新的警告(ruff 通过)
  • 我已经添加了覆盖我更改的测试(21 项行为级验证,红→绿双向,见 Testing)
  • 我已经相应地更新了文档(如果适用)(无面向用户的配置变更,无需文档更新)
  • 我遵循了这个项目的代码风格(ruff 通过,注释风格与现有代码一致)

Fixes #471
Helps #475

qinzihan added 5 commits November 10, 2026 09:33
…deadlock

add_recording/remove_recording/clear_all_recordings are coroutines that
await disk writes while holding the lock. With a threading.Lock, another
recording operation started during that await window blocks the entire
event-loop thread — the holder can never resume to release the lock, so
the whole app freezes (UI unresponsive, monitoring stops, only a process
kill recovers). Verified with a repro: the old pattern deadlocks the loop
deterministically while an asyncio.Lock completes the same workload.
An empty or JSON-corrupt user config (disk glitch, sync conflict, bad
manual edit) evaluated falsy and was silently overwritten by the default
template, destroying all user settings with no recovery. The file is now
copied to user_settings.json.bak (with a warning log) before rebuilding
from defaults; valid configs are untouched.
send_to_telegram returned success unconditionally: _async_post swallows
every exception into {'error': ...} and the Telegram API reports rejections
as HTTP 200 + {'ok': false}, so a wrong token/chat_id or dead proxy still
logged 'pushed successfully'. The response is now checked — {'ok': true}
is required, otherwise the failure (with description) is reported.
Two related stuck-state bugs that made a room unrecoverable until app
restart:

- start_ffmpeg/start_direct_download never unregistered the recorder on
  their exception paths (e.g. ffmpeg missing, spawn failure). The rec_id
  stayed in active_recorders forever, and since check_if_live skips rooms
  present in that dict — while replacing an entry also requires going
  through check_if_live — the room was permanently skipped.

- start_recording's preparation block (filename/output dir/makedirs) ran
  before any try/except while start_update() had already flipped
  is_recording=True. Any failure there was silently swallowed by the
  fire-and-forget runner, leaving a fake 'recording' card with no file
  (as reported in ihmily#471) and blocking monitoring forever. The block now
  resets is_recording, marks the room as errored and unregisters.

Also make remove_active_recorder unregister only itself (identity check),
so a stale recorder exiting late cannot drop the registration of the
newer recorder that replaced it.
DirectStreamDownloader swallowed every failure (expired URL / HTTP 403,
DNS error, proxy failure) with a log line, and start_direct_download
treated any finished task as success: empty/partial files produced a
'Direct Downloading Completed' push notification. The downloader now
records the failure (self.error) and the caller marks the room as
RECORDING_ERROR without the completion notification, then lets
recheck_live_status retry with a fresh stream URL. Manual stops keep the
previous behavior.
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.0.3桌面版录制油管直播间,显示录制实际保存文件夹是空的

1 participant