From 3f11d9cdb65eecd692a959109ab5df9eeef354b9 Mon Sep 17 00:00:00 2001 From: 21pages Date: Mon, 15 Jul 2024 10:49:09 +0800 Subject: [PATCH] remove Instant sub (#8714) which cause crash when connect to windows just startup Signed-off-by: 21pages --- src/lan.rs | 6 +++--- src/server/input_service.rs | 12 +++++++++--- src/virtual_display_manager.rs | 10 ++++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/lan.rs b/src/lan.rs index bd337ba97..666e73c7c 100644 --- a/src/lan.rs +++ b/src/lan.rs @@ -283,7 +283,7 @@ async fn handle_received_peers(mut rx: UnboundedReceiver) }); let mut response_set = HashSet::new(); - let mut last_write_time = Instant::now() - std::time::Duration::from_secs(4); + let mut last_write_time: Option = None; loop { tokio::select! { data = rx.recv() => match data { @@ -297,11 +297,11 @@ async fn handle_received_peers(mut rx: UnboundedReceiver) } } peers.insert(0, peer); - if last_write_time.elapsed().as_millis() > 300 { + if last_write_time.map(|t| t.elapsed().as_millis() > 300).unwrap_or(true) { config::LanPeers::store(&peers); #[cfg(feature = "flutter")] crate::flutter_ffi::main_load_lan_peers(); - last_write_time = Instant::now(); + last_write_time = Some(Instant::now()); } } None => { diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 245c86244..30a885663 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -312,7 +312,7 @@ pub fn new_window_focus() -> GenericService { fn update_last_cursor_pos(x: i32, y: i32) { let mut lock = LATEST_SYS_CURSOR_POS.lock().unwrap(); if lock.1 .0 != x || lock.1 .1 != y { - (lock.0, lock.1) = (Instant::now(), (x, y)) + (lock.0, lock.1) = (Some(Instant::now()), (x, y)) } } @@ -411,7 +411,7 @@ lazy_static::lazy_static! { }; static ref KEYS_DOWN: Arc>> = Default::default(); static ref LATEST_PEER_INPUT_CURSOR: Arc> = Default::default(); - static ref LATEST_SYS_CURSOR_POS: Arc> = Arc::new(Mutex::new((Instant::now().sub(MOUSE_MOVE_PROTECTION_TIMEOUT), (INVALID_CURSOR_POS, INVALID_CURSOR_POS)))); + static ref LATEST_SYS_CURSOR_POS: Arc, (i32, i32))>> = Arc::new(Mutex::new((None, (INVALID_CURSOR_POS, INVALID_CURSOR_POS)))); } static EXITING: AtomicBool = AtomicBool::new(false); @@ -808,7 +808,13 @@ fn active_mouse_(conn: i32) -> bool { true /* this method is buggy (not working on macOS, making fast moving mouse event discarded here) and added latency (this is blocking way, must do in async way), so we disable it for now // out of time protection - if LATEST_SYS_CURSOR_POS.lock().unwrap().0.elapsed() > MOUSE_MOVE_PROTECTION_TIMEOUT { + if LATEST_SYS_CURSOR_POS + .lock() + .unwrap() + .0 + .map(|t| t.elapsed() > MOUSE_MOVE_PROTECTION_TIMEOUT) + .unwrap_or(true) + { return true; } diff --git a/src/virtual_display_manager.rs b/src/virtual_display_manager.rs index f03121967..37bfeb334 100644 --- a/src/virtual_display_manager.rs +++ b/src/virtual_display_manager.rs @@ -428,7 +428,7 @@ pub mod amyuni_idd { lazy_static::lazy_static! { static ref LOCK: Arc> = Default::default(); - static ref LAST_PLUG_IN_HEADLESS_TIME: Arc> = Arc::new(Mutex::new(Instant::now().sub(Duration::from_secs(100)))); + static ref LAST_PLUG_IN_HEADLESS_TIME: Arc>> = Arc::new(Mutex::new(None)); } fn get_deviceinstaller64_work_dir() -> ResultType>> { @@ -573,10 +573,12 @@ pub mod amyuni_idd { pub fn plug_in_headless() -> ResultType<()> { let mut tm = LAST_PLUG_IN_HEADLESS_TIME.lock().unwrap(); - if tm.elapsed() < Duration::from_secs(3) { - bail!("Plugging in too frequently."); + if let Some(tm) = &mut *tm { + if tm.elapsed() < Duration::from_secs(3) { + bail!("Plugging in too frequently."); + } } - *tm = Instant::now(); + *tm = Some(Instant::now()); drop(tm); let mut is_async = false;