Skip to content

fix(whp): helper 防孤兒——Job Object + stdin-EOF 自我了結#126

Merged
TimLai666 merged 2 commits into
mainfrom
claude/magical-liskov-d0486b
Jul 11, 2026
Merged

fix(whp): helper 防孤兒——Job Object + stdin-EOF 自我了結#126
TimLai666 merged 2 commits into
mainfrom
claude/magical-liskov-d0486b

Conversation

@TimLai666

Copy link
Copy Markdown
Owner

問題

與 macOS vz 實機發現的同款孤兒化問題(vz 修法在 claude/inspiring-chaum-208d84 分支待合併):whp.rs spawn chefer-whp-helper 時只 pipe stdout,無 Job Object、無 stdin liveness。runtime 的 Ctrl+C 處理仰賴「helper 與 runtime 同 console、console 事件同時送達」,因此對 runtime 單殺taskkill /PID <runtime>,非 console Ctrl+C)時 helper 收不到任何訊號,micro-VM 會殘留繼續執行。

誠實標註:程式碼層面確認原本無任何防護;但手邊無 Windows 實機,孤兒化尚未實際重現,本修復是依 vz 實機重現過的同款機制推定。

修法(雙保險,DESIGN §6 whp 新增「Helper 生命週期」契約段)

  1. Job Objectwhp.rs):spawn 後立刻把 helper 掛進 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 的 job 並故意洩漏 handle——runtime 無論怎麼死(taskkill /F、當機、正常結束)OS 都會關閉 handle → job 關閉 → helper 連同 VM 被系統終結。Windows 8+ 支援巢狀 job(runtime 已在別的 job 如 CI runner 也掛得進);掛入失敗僅警告,由 2. 後援。
  2. stdin liveness(與 vz 同契約;whp.rs + whp-helper/src/main.rs):runtime 以 piped stdin spawn 並 mem::forget 寫端讓 fd 隨行程存亡;helper boot 模式以專屬執行緒讀 stdin,EOF 即自我了結(VM 在行程內,exit 即拆)。與 vz 的差異:WHP guest console 無輸入路徑(serial 模擬僅 TX),讀到的資料直接丟棄、無 terminal stdin 泵送;--preflight/--gui-selftest.output() 驅動(stdin 立即 EOF),安裝監看以免誤殺。

兩機制皆不觸發 vdb data 回寫(M3 既有限制:僅 guest 乾淨關機回寫),與被 kill 的行為一致。

測試

  • whp.rskill_on_close_job_terminates_child_when_handle_closes——以長命 dummy child 鎖住 KILL_ON_JOB_CLOSE 語意(CI 的 Windows runner 執行,不需 WHP)。
  • whp-helperdrain_until_eof_* 兩則純邏輯測試(跨平台)鎖住「Interrupted 重試、資料丟棄、EOF/讀錯即返回」。
  • 本機(macOS)驗證:cargo test 全 workspace 通過;cargo check/clippy --target x86_64-pc-windows-msvc -p vmm-backend -p whp-helper --tests 乾淨(Windows-only 程式碼含測試皆過編譯與 lint)。

實機待驗項(無 Windows 實機,標註於 AGENTS.md Follow-ups)

  • 端到端孤兒化重現與修復驗證:CHEFER_BACKEND=whp 跑 bundle → taskkill /F /PID <runtime> → 確認 chefer-whp-helper 數秒內消失、無殘留。
  • 正常路徑迴歸:真 WHP VM 開機、guest exit code 傳播、GUI 模式不受 stdin pipe 化影響(程式碼層面 helper 原本就不讀 stdin,推定無影響)。

合併注意

vz 分支(claude/inspiring-chaum-208d84)AGENTS.md 的「Windows whp-helper 疑有同款孤兒化問題」follow-up 由本 PR 解決,兩邊都合併時請刪除該項(AGENTS.md 的 ## Follow-ups 區塊兩分支都有新增,預期有一個小衝突)。

🤖 Generated with Claude Code

TimLai666 and others added 2 commits July 11, 2026 20:05
與 macOS vz 實機發現的同款問題(vz 修法在 claude/inspiring-chaum-208d84 分支
待合併):whp.rs spawn chefer-whp-helper 只 pipe stdout,runtime 的 Ctrl+C 處理
仰賴 helper 同 console 收到同一事件——對 runtime 單殺(taskkill /PID,非 console
Ctrl+C)時 helper 收不到任何訊號,micro-VM 推測會殘留繼續執行(程式碼層面確認
無任何防護;無 Windows 實機,尚未實際重現)。

雙保險修法(DESIGN §6 whp 新增「Helper 生命週期」契約段):

1. Job Object:spawn 後把 helper 掛進 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 的
   job 並故意洩漏 handle——runtime 無論怎麼死(taskkill /F、當機、正常結束)
   OS 都會關閉 handle → job 關閉 → helper 連同 VM 被系統終結。掛入失敗僅
   警告,由 2. 後援。
2. stdin liveness(與 vz 同契約):runtime 以 piped stdin spawn 並 mem::forget
   寫端讓 fd 隨行程存亡;helper boot 模式以專屬執行緒讀 stdin,EOF 即自我了結。
   WHP guest console 無輸入路徑(serial 僅 TX),資料直接丟棄、無 vz 的
   terminal stdin 泵送;--preflight/--gui-selftest(.output() 驅動、stdin 立即
   EOF)不安裝監看。

測試:whp.rs 新增 KILL_ON_JOB_CLOSE 語意測試(dummy child,CI Windows runner
執行);whp-helper 新增 drain_until_eof 純邏輯測試(跨平台)。真 WHP VM 的
端到端孤兒化重現與修復驗證待實機(AGENTS.md Follow-ups)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@TimLai666
TimLai666 merged commit 5f61d37 into main Jul 11, 2026
18 checks passed
@TimLai666
TimLai666 deleted the claude/magical-liskov-d0486b branch July 11, 2026 12:52
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