From bed6cfa2066c8e0d1b552387d7ac2620422d60cb Mon Sep 17 00:00:00 2001 From: Malloc Voidstar <1284317+AlyoshaVasilieva@users.noreply.github.com> Date: Fri, 10 Dec 2021 10:47:12 -0800 Subject: [PATCH] Exit Android initialization loop with error when stuck 500ms is fairly arbitrary; my emulator took 35 to run that loop. --- src/backends/android.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/backends/android.rs b/src/backends/android.rs index 55449c3..c47d86a 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -4,7 +4,7 @@ use std::ffi::{CStr, CString}; use std::os::raw::c_void; use std::sync::{Mutex, RwLock}; use std::thread; -use std::time::Duration; +use std::time::{Duration, Instant}; use jni::objects::{GlobalRef, JObject, JString}; use jni::sys::{jfloat, jint, JNI_VERSION_1_6}; @@ -198,12 +198,18 @@ impl Android { } let tts = env.new_global_ref(tts)?; // This hack makes my brain bleed. + const MAX_WAIT_TIME: Duration = Duration::from_millis(500); + let start = Instant::now(); + // Wait a max of 500ms for initialization, then return an error to avoid hanging. loop { { let pending = PENDING_INITIALIZATIONS.read().unwrap(); if !(*pending).contains(&bid) { break; } + if start.elapsed() > MAX_WAIT_TIME { + return Err(Error::OperationFailed); + } } thread::sleep(Duration::from_millis(5)); }