From fa85c8ebe392060621e47f433147dbcbd4292b91 Mon Sep 17 00:00:00 2001 From: Yashashwini2003 <138598494+Yashashwini2003@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:01:09 +0000 Subject: [PATCH] add auto destruction for windows --- screenpipe-app-tauri/src-tauri/src/main.rs | 13 ------- screenpipe-app-tauri/src-tauri/src/sidecar.rs | 34 +++++++++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/screenpipe-app-tauri/src-tauri/src/main.rs b/screenpipe-app-tauri/src-tauri/src/main.rs index d7d68361d..97f96fe2b 100755 --- a/screenpipe-app-tauri/src-tauri/src/main.rs +++ b/screenpipe-app-tauri/src-tauri/src/main.rs @@ -11,7 +11,6 @@ use std::env; use std::fs; use std::fs::File; use std::path::PathBuf; -use std::process; use std::sync::Arc; use tauri::Config; use tauri::Manager; @@ -120,18 +119,6 @@ async fn main() { .app_handle() .set_activation_policy(tauri::ActivationPolicy::Regular); } - - #[cfg(target_os = "windows")] - { - // Kill all screenpipe processes (CLI sidecar) - if let Err(e) = sidecar::kill_all_sreenpipes( - app_handle.state::(), - app_handle.clone(), - ){ - error!("Failed to kill sidecar: {}", e); - } - } - window.hide().unwrap(); api.prevent_close(); } diff --git a/screenpipe-app-tauri/src-tauri/src/sidecar.rs b/screenpipe-app-tauri/src-tauri/src/sidecar.rs index 4c7e41984..184ec1d5e 100644 --- a/screenpipe-app-tauri/src-tauri/src/sidecar.rs +++ b/screenpipe-app-tauri/src-tauri/src/sidecar.rs @@ -2,6 +2,7 @@ use crate::{get_base_dir, SidecarState}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::env; +use std::process; use std::sync::Arc; use std::time::{Duration, Instant}; use tauri::async_runtime::JoinHandle; @@ -103,6 +104,39 @@ pub async fn kill_all_sreenpipes( } } +#[cfg(target_os = "windows")] +pub async fn auto_destruct_monitor(auto_destruct_pid: Option) { + if let Some(pid) = auto_destruct_pid { + loop { + if !is_process_alive(pid) { + tracing::info!("Parent process with PID {} is not alive. Terminating sidecar.", pid); + process::exit(0); + } + sleep(Duration::from_secs(1)).await; + } + } +} + +#[cfg(target_os = "windows")] +fn is_process_alive(pid: u32) -> bool { + use windows::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_INFORMATION}; + use windows::Win32::Foundation::{CloseHandle, HANDLE}; + + unsafe { + let process: HANDLE = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid); + if process.is_invalid() { + return false; + } + CloseHandle(process); + true + } +} + +#[cfg(not(target_os = "windows"))] +fn is_process_alive(pid: u32) -> bool { + unsafe { libc::kill(pid as i32, 0) == 0 } +} + #[tauri::command] pub async fn spawn_screenpipe( state: tauri::State<'_, SidecarState>,