修复 macOS 锁屏恢复后 helper 后端误断开【Fix macOS helper reinjection after sleep】#105
Merged
Merged
Conversation
bd1e769 to
cbe85ed
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
macOS 下 Codex++ 在锁屏、睡眠或恢复后,顶部状态偶尔会变红,表现为“后端已断开”。
排查发现,这个问题不一定是 helper server 真的退出,而是 renderer 里的注入 bridge 在恢复后丢失或 CDP WebSocket 被重置,导致前端无法继续调用 helper。
现场现象
出现红灯时,本地 helper 和 Codex CDP 端口仍然可能正常:
但在 Codex renderer DevTools 中检查:
这说明 helper 进程和 CDP 还在,但页面里的 bridge 已经不存在了。
相关日志
锁屏恢复后,曾观察到 bridge WebSocket 被系统重置,随后重新注入成功:
此前也观察到 macOS 存活判断误判后关闭 helper:
原因分析
macOS 版 launcher 启动 Codex 后拿不到一个可等待的 Codex 子进程句柄,所以原逻辑主要依赖
ps命令行匹配来判断 Codex 是否仍在运行。这个判断在 macOS/Electron 场景下比较脆弱:
open -a启动,launcher 不持有真实子进程因此会出现两类问题:
修改内容
本次改动主要做了三件事:
macOS 存活判断优先使用 CDP page target
先通过当前 debug port 检查是否还能看到 Codex page target;只有 CDP 不可用时,才回退到
ps进程匹配。helper 端口已存在时复用健康实例
启动前先检查
127.0.0.1:<helper_port>/health。如果已有 helper 正常运行,则复用该 helper,避免因为端口已占用直接启动失败。增加 renderer bridge watchdog
注入成功后定期检查 renderer 里的
window.__codexSessionDeleteBridge是否还存在。如果 bridge 丢失或 CDP WebSocket 被重置,会尝试重新注入。同时补充了关键运行日志,后续如果再次出现红灯,可以通过:
tail -n 120 ~/.codex-session-delete/launcher.log判断是 helper 退出、Codex liveness 误判,还是 bridge 丢失后重注入失败。
影响范围
该修复主要影响 macOS launcher 路径:
codex_proc is None时,会使用新的 CDP 优先存活判断/health正常时生效,不会复用不可用端口验证
本地测试通过:
结果:
本地 macOS 锁屏恢复后观察到 bridge 可自动重注入,红灯状态可以恢复。