Skip to content

fix: 忽略过期启动检查失败 - #5221

Closed
eyaeya wants to merge 1 commit into
vernesong:devfrom
eyaeya:net/start-generation-guard
Closed

eyaeya wants to merge 1 commit into
vernesong:devfrom
eyaeya:net/start-generation-guard

Conversation

@eyaeya

@eyaeya eyaeya commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Dependency chain

建议在 #5220 之后合并。#5220 先修正 core/watchdog instance 状态判断,本 PR 再处理旧启动检查反向影响新启动的问题。

问题现象

start_service 会把 check_core_status "start" 放到后台等待 core、TUN 或控制接口就绪。如果用户或定时任务在旧检查还没结束时再次 restart,旧检查仍会使用全局 pidof clash 和当前网络状态判断。旧检查超时后会调用 start_fail,从而把 openclash.config.enable 置 0 并 stop 当前服务,可能反向停止已经成功的新一轮启动。

根因

后台状态检查没有与启动 generation 绑定。旧检查无法判断自己对应的启动轮次是否已经被新的 start/restart 取代。

证据

  • start_service 原先在 Step 6 后台运行 check_core_status "start" &
  • check_core_status 超时路径会调用 start_fail
  • start_fail 会设置 enable=0 并执行 stop
  • 新增 tests/openclash_start_generation_test.sh 覆盖 start generation 文件、后台传参、stale failure 跳过和 stop 清理。

修复方案

  • 新增 /tmp/openclash_start_id,每次 start 生成当前轮次 token。
  • 后台 check_core_status 接收该 token。
  • start_fail 在带 token 的后台检查中先确认 token 仍是当前轮次;如果已被新 start/restart 替换,则只记录并退出,不再停用/停止当前服务。
  • stop_service 清理 generation 文件,让停止期间遗留的后台检查自动变成 stale。

为什么没有扩大范围

本 PR 不改变 core 启动命令、respawn、TUN/API 等待条件、防火墙规则或 DNS 设置。没有把所有启动流程串行化,也没有改前台配置失败的处理;只防止旧后台检查影响新一轮服务。

与已有开启态 PR 的关系

验证

  • bash -n luci-app-openclash/root/usr/share/openclash/*.sh:通过
  • bash -n luci-app-openclash/root/etc/init.d/openclash:通过
  • bash -n tests/*.sh:通过
  • for t in tests/*.sh; do bash "$t"; done:全部通过
  • git diff --check:通过
  • rg -n '^(<<<<<<<|=======|>>>>>>>)':无匹配

未做 live router 写入验证。

剩余风险

该 guard 只保护带 generation token 的后台 core 状态检查。手动执行的 firewall/manual/restore 检查仍沿用原先行为,因为它们不是 start/restart 交叠导致的旧轮次问题。

@eyaeya

eyaeya commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

这个拆分 PR 已被新的合并版 PR 取代:#5230

原因:#5220#5221#5225 都属于 OpenClash 启停生命周期竞态修复,并且同时处理时会在 luci-app-openclash/root/etc/init.d/openclash 产生真实冲突。新的合并版 PR 保留了本 PR 的行为,同时把 core/watchdog 状态、启动 generation guard、生命周期锁统一整理成单个 reviewer 友好的提交。

已在合并版 PR 中验证:

  • focused tests
  • init.d/openclash 与 watchdog 脚本语法检查
  • git diff --check
  • conflict marker scan
  • 多轮对抗式复核,最终无修改点

@eyaeya eyaeya closed this Jul 8, 2026
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