fix: phserver.sh 加 flock 互斥,防 PH 监控重复拉起导致 sunshine 双实例

开机场景:phserver.sh 在 refresh-xauth --wait 45 等待登录期间,PH 监控
(15 秒周期)判 sunshine 不在再次 Start 本脚本 → 双 sunshine 实例竞争端口
(后起者 47990 bind 冲突退出,存活实例 nvhttp 47984/47989 端口不完整),
moonlight 无法发现主机(145 重启后实测)。锁在脚本同目录,sunshine 存活
期间(含等待)持有,重复调用 10ms 内静默退出。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Zhanghu
2026-11-06 21:46:56 +08:00
parent 6a5af3b2cf
commit 5acfece5b8

View File

@@ -14,6 +14,17 @@ fi
# Ensure AppImage has execute permission
chmod +x "${APPIMAGE}"
# 互斥锁:sunshine 运行期间(含下方 --wait 等待登录)持锁,阻止重复拉起。
# PH 监控约 15 秒判一次 not running:若本脚本还在 --wait 等登录,PH 会再 Start 一个,
# 双实例竞争端口(47990 bind 冲突退出、存活实例 nvhttp 端口不完整),moonlight 无法发现。
# 锁放脚本同目录(部署目录):unit 的 ProtectSystem=strict 已放行其读写,
# 且不受 PrivateTmp 影响(服务与手动执行共享同一把锁)。
exec 9>"${SCRIPT_DIR}/phserver.lock"
if ! flock -n 9; then
echo "phserver.sh already running (lock held), exit"
exit 0
fi
# 每次拉起 sunshine 前刷新 /root/.Xauthority(最多等 45 秒登录就绪):
# 自愈开机竞态——服务 ExecStartPre 时机可能早于用户登录,cookie 是上次会话的,
# 会导致 sunshine capture 初始化失败、串流不可用。脚本不存在/失败不阻断启动。