feat: watchdog to kill gdcef_helper when the parent process accidently exits#205
Draft
dsh0416 wants to merge 1 commit into
Draft
feat: watchdog to kill gdcef_helper when the parent process accidently exits#205dsh0416 wants to merge 1 commit into
dsh0416 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a parent-process watchdog to gdcef_helper subprocesses so they can self-terminate when their launching (browser) process exits, reducing the risk of orphaned helper processes.
Changes:
- Introduces a
gdcef-parent-pidcommand-line switch set by the browser process for child processes. - Adds a new
gdcef_helper::watchdogmodule with platform-specific parent-liveness checks (Linux/macOS/Windows + unsupported fallback). - Spawns a background watchdog loop (currently via a Tokio runtime) to periodically exit when the parent is detected as gone.
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/gdcef_helper/src/watchdog/mod.rs | Adds watchdog entry point that parses the PID switch and starts platform + runtime components. |
| crates/gdcef_helper/src/watchdog/runtime.rs | Adds watchdog thread + polling loop implementation (Tokio-based). |
| crates/gdcef_helper/src/watchdog/linux.rs | Linux parent monitoring via prctl(PR_SET_PDEATHSIG) and /proc existence checks. |
| crates/gdcef_helper/src/watchdog/macos.rs | macOS parent monitoring via kill(pid, 0). |
| crates/gdcef_helper/src/watchdog/windows.rs | Windows parent monitoring via OpenProcess + GetExitCodeProcess. |
| crates/gdcef_helper/src/watchdog/unsupported.rs | Fallback behavior for unsupported platforms. |
| crates/gdcef_helper/src/main.rs | Starts the watchdog for non-browser (child) processes before executing CEF subprocess logic. |
| crates/gdcef_helper/Cargo.toml | Adds tokio and libc (per-target) dependencies for watchdog support. |
| crates/cef_app/src/lib.rs | Defines the shared GDCEF_PARENT_PID_SWITCH constant. |
| crates/cef_app/src/browser_process.rs | Appends the parent PID switch when launching child processes. |
| Cargo.toml | Adds tokio to workspace dependencies. |
| Cargo.lock | Locks in the new Tokio dependency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| eprintln!("Failed to arm gdcef Linux parent-death signal"); | ||
| } | ||
|
|
||
| if !is_parent_alive(parent_pid) { |
Comment on lines
+15
to
+16
| pub(super) fn is_parent_alive(parent_pid: u32) -> bool { | ||
| Path::new("/proc").join(parent_pid.to_string()).exists() |
| } | ||
|
|
||
| pub(super) fn is_parent_alive(parent_pid: u32) -> bool { | ||
| unsafe { libc::kill(parent_pid as libc::pid_t, 0) == 0 } |
Comment on lines
+1
to
+23
| use windows::Win32::Foundation::CloseHandle; | ||
| use windows::Win32::System::Threading::{ | ||
| GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, | ||
| }; | ||
|
|
||
| const STILL_ACTIVE_EXIT_CODE: u32 = 259; | ||
|
|
||
| pub(super) fn prepare_parent_watchdog(parent_pid: u32) { | ||
| if !is_parent_alive(parent_pid) { | ||
| eprintln!("gdcef parent process {parent_pid} exited before watchdog startup"); | ||
| std::process::exit(0); | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn is_parent_alive(parent_pid: u32) -> bool { | ||
| let Ok(handle) = (unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, parent_pid) }) | ||
| else { | ||
| return false; | ||
| }; | ||
|
|
||
| let mut exit_code = 0; | ||
| let is_alive = unsafe { GetExitCodeProcess(handle, &mut exit_code) }.is_ok() | ||
| && exit_code == STILL_ACTIVE_EXIT_CODE; |
Comment on lines
+1
to
+30
| use std::time::Duration; | ||
|
|
||
| use tokio::runtime::Builder; | ||
| use tokio::time::{MissedTickBehavior, interval}; | ||
|
|
||
| const WATCHDOG_INTERVAL: Duration = Duration::from_secs(1); | ||
|
|
||
| pub(super) fn spawn_parent_watchdog(parent_pid: u32) { | ||
| let thread = std::thread::Builder::new().name("gdcef-parent-watchdog".to_string()); | ||
| if let Err(err) = thread.spawn(move || { | ||
| let runtime = match Builder::new_current_thread().enable_time().build() { | ||
| Ok(runtime) => runtime, | ||
| Err(err) => { | ||
| eprintln!("Failed to start gdcef parent watchdog runtime: {err}"); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| runtime.block_on(async move { | ||
| let mut ticker = interval(WATCHDOG_INTERVAL); | ||
| ticker.set_missed_tick_behavior(MissedTickBehavior::Delay); | ||
|
|
||
| loop { | ||
| ticker.tick().await; | ||
| if !super::platform::is_parent_alive(parent_pid) { | ||
| eprintln!("gdcef parent process {parent_pid} exited; helper is exiting"); | ||
| std::process::exit(0); | ||
| } | ||
| } | ||
| }); |
Comment on lines
+45
to
+46
| platform::prepare_parent_watchdog(parent_pid); | ||
| runtime::spawn_parent_watchdog(parent_pid); |
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.
No description provided.