fix, windows try kill flutter main window process only when --server's ipc is (#8086)

occupied

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2024-05-18 23:14:42 +08:00 committed by GitHub
parent 96f41fcc02
commit d3eaa6600d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 10 deletions

View File

@ -356,8 +356,6 @@ async fn handle(data: Data, stream: &mut Connection) {
crate::server::input_service::fix_key_down_timeout_at_exit(); crate::server::input_service::fix_key_down_timeout_at_exit();
if is_server() { if is_server() {
let _ = privacy_mode::turn_off_privacy(0, Some(PrivacyModeState::OffByPeer)); 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); std::process::exit(0);
} }
@ -972,6 +970,12 @@ pub async fn test_rendezvous_server() -> ResultType<()> {
Ok(()) Ok(())
} }
#[tokio::main(flavor = "current_thread")]
pub async fn test_ipc_connection() -> ResultType<()> {
connect(1000, "").await?;
Ok(())
}
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
pub async fn send_url_scheme(url: String) -> ResultType<()> { pub async fn send_url_scheme(url: String) -> ResultType<()> {
connect(1_000, "_url") connect(1_000, "_url")

View File

@ -65,13 +65,13 @@ use windows_service::{
use winreg::enums::*; use winreg::enums::*;
use winreg::RegKey; 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<DisplayInfo>) -> Option<usize> { pub fn get_focused_display(displays: Vec<DisplayInfo>) -> Option<usize> {
unsafe { unsafe {
let hWnd = GetForegroundWindow(); let hwnd = GetForegroundWindow();
let mut rect: RECT = mem::zeroed(); 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; return None;
} }
displays.iter().position(|display| { displays.iter().position(|display| {
@ -2423,19 +2423,23 @@ pub fn is_x64() -> bool {
} }
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
pub fn kill_flutter_main_window() { pub fn try_kill_flutter_main_window_process() {
// It is used to kill the hidden flutter main window process, it can also kill the install window process // It's called when --server failed to start ipc, because the ipc may be occupied by the main window process.
log::info!("kill flutter main window"); // 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 { unsafe {
let window_name = wide_string(&crate::get_app_name()); let window_name = wide_string(&crate::get_app_name());
let class_name = wide_string(FLUTTER_RUNNER_WIN32_WINDOW_CLASS); let class_name = wide_string(FLUTTER_RUNNER_WIN32_WINDOW_CLASS);
let hwnd = FindWindowW(class_name.as_ptr(), window_name.as_ptr()); let hwnd = FindWindowW(class_name.as_ptr(), window_name.as_ptr());
if hwnd.is_null() { if hwnd.is_null() {
log::info!("not found flutter main window");
return; return;
} }
let mut process_id: u32 = 0; let mut process_id: u32 = 0;
GetWindowThreadProcessId(hwnd, &mut process_id as *mut u32); GetWindowThreadProcessId(hwnd, &mut process_id as *mut u32);
if process_id == 0 { if process_id == 0 {
log::info!("failed to get flutter window process id");
return; return;
} }
let output = Command::new("taskkill") let output = Command::new("taskkill")
@ -2445,9 +2449,9 @@ pub fn kill_flutter_main_window() {
.output() .output()
.expect("Failed to execute command"); .expect("Failed to execute command");
if output.status.success() { if output.status.success() {
log::info!("kill flutter main window success"); log::info!("kill flutter main window process success");
} else { } else {
log::error!("kill flutter main window failed"); log::error!("kill flutter main window process failed");
} }
} }
} }

View File

@ -470,6 +470,11 @@ pub async fn start_server(is_server: bool) {
std::thread::spawn(move || { std::thread::spawn(move || {
if let Err(err) = crate::ipc::start("") { if let Err(err) = crate::ipc::start("") {
log::error!("Failed to start ipc: {}", err); 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); std::process::exit(-1);
} }
}); });