fix: Sidecar の orphan process 化を防止 (parent process 監視) (Issue #7) - #164
Conversation
検証中に SIGKILL でデスクトップ main プロセスを強制終了した際、Bun sidecar プロセスが orphan 化 (PPID=1 launchd) して残存する問題を発見。 これまでの実装 (apps/desktop/src-tauri/src/lib.rs): - on_window_event(WindowEvent::Destroyed) で sidecar.kill() するのみ - 正常終了 (ユーザーがウィンドウを閉じる) では機能 - SIGKILL / Rust panic / OOM などの異常終了では event handler が走らず sidecar が孤児プロセスとして残る - macOS には Linux の prctl(PR_SET_PDEATHSIG) 相当機構が無いため、 親プロセス死亡時に子プロセスへ自動通知できない 実機検証 (Phase 2): - main PID 5633、sidecar PID 14247 (PPID=5633) - kill -9 5633 → main GONE、sidecar 生存 (PPID=1 launchd へ reparent 確認) - ユーザーが Force Quit する度に sidecar leak、port/DB ロック保持 修正内容: apps/desktop/src-tauri/src/lib.rs: - sidecar spawn 時に std::process::id() を取得し AITUBERFLOW_PARENT_PID env で渡す apps/server-ts/src/index.ts: - 起動時に AITUBERFLOW_PARENT_PID を読み取り - setInterval(1秒) で process.kill(parentPid, 0) (signal 0 = 存在チェックのみ) - ESRCH (親死亡) を catch したら process.exit(0) で自殺 - .unref() で event loop に余計なロックを掛けない 検証: - cargo check 成功 (7.85s, dev profile) - 親監視ロジックを isolated test で検証: - 非存在 PID (999999) を渡して 1002ms で死亡検出 + exit 確認 - 動作: Force Quit 後 最大 1秒以内 (平均 0.5秒) で sidecar も終了 副次影響: - パフォーマンス: 1秒ごと kill(pid, 0) syscall 1回 = 無視できるレベル - 既存の正常終了経路 (WindowEvent::Destroyed) は変更なし、両方の経路で cleanup - AITUBERFLOW_PARENT_PID env が未設定 (sidecar を直接 bun run で起動した場合等) なら従来通り = stand-alone server モード互換 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
概要
検証中に SIGKILL でデスクトップ main プロセスを強制終了した際、Bun sidecar
プロセスが orphan 化 (PPID=1 launchd) して残存する問題を発見、修正します。
問題
これまでの実装 (apps/desktop/src-tauri/src/lib.rs):
prctl(PR_SET_PDEATHSIG)相当機構が無いため、親プロセス死亡時に子プロセスへ自動通知できない
実機検証結果
ユーザーが Force Quit / Activity Monitor 強制終了 / アプリクラッシュする度に
sidecar leak、port/DB ロック保持、メモリリーク発生。
修正内容
apps/desktop/src-tauri/src/lib.rs
apps/server-ts/src/index.ts
動作
検証
cargo check(apps/desktop/src-tauri) 成功 (7.85s, dev profile clean compile)npm run dev:desktop→ ps で sidecar の PPID=main 確認kill -9 <main_pid>→ 1秒以内に sidecar も終了することを確認副次影響
kill(pid, 0)syscall 1回 = 無視できるレベルAITUBERFLOW_PARENT_PIDenv が未設定 (sidecar を直接 bun run で起動した場合等) なら従来通り、stand-alone server モード互換
ロールバック
revert で完全復元可能。データ・成果物への影響なし。
関連