Skip to content

Commit 37625ec

Browse files
committed
Fix deadlock on activity onDestroy
Fix a deadlock that occurs when an activity is destroyed without process termination, such as when an activity is destroyed and recreated due to a configuration change. The deadlock occurs because `notify_destroyed` blocks until `destroyed` is set to `true`. This only occurs when `WaitableNativeActivityState` is dropped, but the `WaitableNativeActivityState` instance is the very thing being used to await for destruction, resulting in a deadlock.
1 parent 0f00a58 commit 37625ec

File tree

1 file changed

+9
-1
lines changed
  • android-activity/src/native_activity

1 file changed

+9
-1
lines changed

android-activity/src/native_activity/glue.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl WaitableNativeActivityState {
372372

373373
unsafe {
374374
guard.write_cmd(AppCmd::Destroy);
375-
while !guard.destroyed {
375+
while guard.running {
376376
guard = self.cond.wait(guard).unwrap();
377377
}
378378

@@ -545,6 +545,12 @@ impl WaitableNativeActivityState {
545545
self.cond.notify_one();
546546
}
547547

548+
pub fn notify_main_thread_stopped_running(&self) {
549+
let mut guard = self.mutex.lock().unwrap();
550+
guard.running = false;
551+
self.cond.notify_one();
552+
}
553+
548554
pub unsafe fn pre_exec_cmd(
549555
&self,
550556
cmd: AppCmd,
@@ -907,6 +913,8 @@ extern "C" fn ANativeActivity_onCreate(
907913

908914
ndk_context::release_android_context();
909915
}
916+
917+
rust_glue.notify_main_thread_stopped_running();
910918
});
911919

912920
// Wait for thread to start.

0 commit comments

Comments
 (0)