Skip to content

feat: watchdog to kill gdcef_helper when the parent process accidently exits#205

Draft
dsh0416 wants to merge 1 commit into
mainfrom
dsh0416/watchdog
Draft

feat: watchdog to kill gdcef_helper when the parent process accidently exits#205
dsh0416 wants to merge 1 commit into
mainfrom
dsh0416/watchdog

Conversation

@dsh0416

@dsh0416 dsh0416 commented May 25, 2026

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings May 25, 2026 03:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-pid command-line switch set by the browser process for child processes.
  • Adds a new gdcef_helper::watchdog module 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);
@dsh0416 dsh0416 marked this pull request as draft May 25, 2026 03:56
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.

2 participants