From f06dd0cd3562382bbcae7329672c4487e34adfba Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Thu, 10 Jul 2025 14:03:59 +0000 Subject: [PATCH] ANDROID: rust_binder: release threads before refs Commit 5da94eacba68 ("ANDROID: rust_binder: print and avoid underflow with invalid refcnt") added a print when decrementing the refcount if the ref doesn't exist. This revealed that this sometimes happens inside thread.release() because the refs were cleared before releasing the threads. In principle this error is harmless, but to avoid printing this error, release threads before clearing refs. Bug: 430660253 Change-Id: I9ceb71ca831f5bb58193e12951245bab19ea4fa2 Signed-off-by: Alice Ryhl --- drivers/android/binder/process.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs index 09bf4dea8d51..c285ec0561bd 100644 --- a/drivers/android/binder/process.rs +++ b/drivers/android/binder/process.rs @@ -1303,6 +1303,13 @@ impl Process { work.into_arc().cancel(); } + // Take all threads and release them. + let threads = take(&mut self.inner.lock().threads); + for thread in threads.values() { + thread.release(); + } + drop(threads); + // Free any resources kept alive by allocated buffers. let omapping = self.inner.lock().mapping.take(); if let Some(mut mapping) = omapping { @@ -1344,18 +1351,8 @@ impl Process { } drop(freeze_listeners); - // Do similar dance for the state lock. - let mut inner = self.inner.lock(); - let threads = take(&mut inner.threads); - let nodes = take(&mut inner.nodes); - drop(inner); - - // Release all threads. - for thread in threads.values() { - thread.release(); - } - // Deliver death notifications. + let nodes = take(&mut self.inner.lock().nodes); for node in nodes.values() { loop { let death = {