From 68b56d9172bbe5e8258f292a1f9ced951806f524 Mon Sep 17 00:00:00 2001 From: Siavash Sameni Date: Tue, 7 Apr 2026 11:40:14 +0400 Subject: [PATCH] fix: ping every 5min (was 5s), clean endpoint on failure, never block connect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ping interval: 5 minutes (was 5 seconds — too aggressive) - Rust ping_relay: explicitly close endpoint + shutdown runtime on failure - Connect button works regardless of ping status (never blocked) - Ping failure doesn't corrupt engine state Co-Authored-By: Claude Opus 4.6 (1M context) --- .../main/java/com/wzp/ui/call/InCallScreen.kt | 5 +++-- crates/wzp-android/src/engine.rs | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/java/com/wzp/ui/call/InCallScreen.kt b/android/app/src/main/java/com/wzp/ui/call/InCallScreen.kt index 17b5f69..1774614 100644 --- a/android/app/src/main/java/com/wzp/ui/call/InCallScreen.kt +++ b/android/app/src/main/java/com/wzp/ui/call/InCallScreen.kt @@ -90,12 +90,13 @@ fun InCallScreen( var showManageRelays by remember { mutableStateOf(false) } - // Periodic ping every 5 seconds while app is open + // Ping once on launch, then every 5 minutes LaunchedEffect(Unit) { viewModel.loadSavedFingerprints() + viewModel.pingAllServers() while (true) { + kotlinx.coroutines.delay(300_000) // 5 minutes viewModel.pingAllServers() - kotlinx.coroutines.delay(5000) } } diff --git a/crates/wzp-android/src/engine.rs b/crates/wzp-android/src/engine.rs index 95d7011..0ef7739 100644 --- a/crates/wzp-android/src/engine.rs +++ b/crates/wzp-android/src/engine.rs @@ -177,19 +177,22 @@ impl WzpEngine { .enable_all() .build()?; - rt.block_on(async { + let result = rt.block_on(async { let bind: SocketAddr = "0.0.0.0:0".parse().unwrap(); let endpoint = wzp_transport::create_endpoint(bind, None)?; let client_cfg = wzp_transport::client_config(); let start = Instant::now(); - let conn = tokio::time::timeout( + let conn_result = tokio::time::timeout( std::time::Duration::from_secs(3), wzp_transport::connect(&endpoint, addr, "ping", client_cfg), ) - .await - .map_err(|_| anyhow::anyhow!("timeout"))??; + .await; + // Always close endpoint to prevent resource leaks + endpoint.close(0u32.into(), b"done"); + + let conn = conn_result.map_err(|_| anyhow::anyhow!("timeout"))??; let rtt_ms = start.elapsed().as_millis() as u64; let server_fp = conn .peer_identity() @@ -203,8 +206,12 @@ impl WzpEngine { .unwrap_or_default(); conn.close(0u32.into(), b"ping"); - Ok(format!(r#"{{"rtt_ms":{},"server_fingerprint":"{}"}}"#, rtt_ms, server_fp)) - }) + Ok::<_, anyhow::Error>(format!(r#"{{"rtt_ms":{},"server_fingerprint":"{}"}}"#, rtt_ms, server_fp)) + }); + + // Shutdown runtime cleanly with timeout + rt.shutdown_timeout(std::time::Duration::from_millis(500)); + result } pub fn set_mute(&self, muted: bool) {