From 9629242dd4b19c974e80cb772eaf7af6d79fae38 Mon Sep 17 00:00:00 2001 From: Tiger Huang Date: Sat, 18 Apr 2020 01:37:14 +0800 Subject: [PATCH] Copy leashes about to return to prevent others release them When a thread is excuting addWindow or relayoutWindow, there might be another thread attempting to call updateInputMethodTargetWindow but waiting for the lock. When the lock is released by addwindow or relayoutWindow, the logic inside updateInputMethodTargetWindow might release the leash that addWindow or relayoutWindow is about to return. This CL return the copied leashes, so that other thread won't release the leash which is about to return. Fix: 154175855 Test: atest CtsWidgetTestCases Change-Id: I48a7dbf1576e07302c66955e2d3c86037e987617 --- .../server/wm/WindowManagerService.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index f55a1b3f6ab32..2887a8ad80c2c 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -2445,17 +2445,15 @@ public class WindowManagerService extends IWindowManager.Stub if (controls != null) { final int length = Math.min(controls.length, outControls.length); for (int i = 0; i < length; i++) { - final InsetsSourceControl control = controls[i]; - - // Check if we are sending invalid leashes. - final SurfaceControl leash = control != null ? control.getLeash() : null; - if (leash != null && !leash.isValid()) { - Slog.wtf(TAG, leash + " is not valid before sending to " + win, - leash.getReleaseStack()); - } - - outControls[i] = win.isClientLocal() && control != null - ? new InsetsSourceControl(control) : control; + // We will leave the critical section before returning the leash to the client, + // so we need to copy the leash to prevent others release the one that we are + // about to return. + // TODO: We will have an extra copy if the client is not local. + // For now, we rely on GC to release it. + // Maybe we can modify InsetsSourceControl.writeToParcel so it can release + // the extra leash as soon as possible. + outControls[i] = controls[i] != null + ? new InsetsSourceControl(controls[i]) : null; } } }