From d3eaa6600d7b1207cc727837861b20129be5f14a Mon Sep 17 00:00:00 2001 From: 21pages Date: Sat, 18 May 2024 23:14:42 +0800 Subject: [PATCH] fix, windows try kill flutter main window process only when --server's ipc is (#8086) occupied Signed-off-by: 21pages --- src/ipc.rs | 8 ++++++-- src/platform/windows.rs | 20 ++++++++++++-------- src/server.rs | 5 +++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 02a5d9429..b24069a73 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -356,8 +356,6 @@ async fn handle(data: Data, stream: &mut Connection) { crate::server::input_service::fix_key_down_timeout_at_exit(); if is_server() { let _ = privacy_mode::turn_off_privacy(0, Some(PrivacyModeState::OffByPeer)); - #[cfg(all(windows, feature = "flutter"))] - crate::platform::kill_flutter_main_window(); } std::process::exit(0); } @@ -972,6 +970,12 @@ pub async fn test_rendezvous_server() -> ResultType<()> { Ok(()) } +#[tokio::main(flavor = "current_thread")] +pub async fn test_ipc_connection() -> ResultType<()> { + connect(1000, "").await?; + Ok(()) +} + #[tokio::main(flavor = "current_thread")] pub async fn send_url_scheme(url: String) -> ResultType<()> { connect(1_000, "_url") diff --git a/src/platform/windows.rs b/src/platform/windows.rs index ab939fe08..a3cc9f38e 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -65,13 +65,13 @@ use windows_service::{ use winreg::enums::*; use winreg::RegKey; -pub const FLUTTER_RUNNER_WIN32_WINDOW_CLASS: &'static str = "FLUTTER_RUNNER_WIN32_WINDOW"; +pub const FLUTTER_RUNNER_WIN32_WINDOW_CLASS: &'static str = "FLUTTER_RUNNER_WIN32_WINDOW"; // main window, install window pub fn get_focused_display(displays: Vec) -> Option { unsafe { - let hWnd = GetForegroundWindow(); + let hwnd = GetForegroundWindow(); let mut rect: RECT = mem::zeroed(); - if GetWindowRect(hWnd, &mut rect as *mut RECT) == 0 { + if GetWindowRect(hwnd, &mut rect as *mut RECT) == 0 { return None; } displays.iter().position(|display| { @@ -2423,19 +2423,23 @@ pub fn is_x64() -> bool { } #[cfg(feature = "flutter")] -pub fn kill_flutter_main_window() { - // It is used to kill the hidden flutter main window process, it can also kill the install window process - log::info!("kill flutter main window"); +pub fn try_kill_flutter_main_window_process() { + // It's called when --server failed to start ipc, because the ipc may be occupied by the main window process. + // When --service quit the ipc process, ipc process will call std::process::exit, std::process::exit not work may be the reason. + // FindWindow not work in --service, https://forums.codeguru.com/showthread.php?169091-FindWindow-in-service + log::info!("try kill flutter main window process"); unsafe { let window_name = wide_string(&crate::get_app_name()); let class_name = wide_string(FLUTTER_RUNNER_WIN32_WINDOW_CLASS); let hwnd = FindWindowW(class_name.as_ptr(), window_name.as_ptr()); if hwnd.is_null() { + log::info!("not found flutter main window"); return; } let mut process_id: u32 = 0; GetWindowThreadProcessId(hwnd, &mut process_id as *mut u32); if process_id == 0 { + log::info!("failed to get flutter window process id"); return; } let output = Command::new("taskkill") @@ -2445,9 +2449,9 @@ pub fn kill_flutter_main_window() { .output() .expect("Failed to execute command"); if output.status.success() { - log::info!("kill flutter main window success"); + log::info!("kill flutter main window process success"); } else { - log::error!("kill flutter main window failed"); + log::error!("kill flutter main window process failed"); } } } diff --git a/src/server.rs b/src/server.rs index 93906cf08..59104e6ba 100644 --- a/src/server.rs +++ b/src/server.rs @@ -470,6 +470,11 @@ pub async fn start_server(is_server: bool) { std::thread::spawn(move || { if let Err(err) = crate::ipc::start("") { log::error!("Failed to start ipc: {}", err); + #[cfg(all(windows, feature = "flutter"))] + if crate::is_server() && crate::ipc::test_ipc_connection().is_ok() { + log::error!("ipc is occupied by another process, try kill it"); + crate::platform::try_kill_flutter_main_window_process(); + } std::process::exit(-1); } });