From acc01ae9f403ebca360aed3c2e1446c37236c508 Mon Sep 17 00:00:00 2001 From: Pinyao Ting Date: Tue, 8 Jun 2021 00:59:51 +0000 Subject: [PATCH] Avoid potential deadlock There has been a few reports indicates ANR was observed when calling ShortcutBitmapSaver#getBitmapPathMayWaitLocked, which timed after 30 seconds. This CL reduces the timeout to 5 seconds as 30 seconds is unreasonably long. Additionally, since ShortcutService#postValue is no longer needed for AppSearch migration, it is better to remove this api to avoid a potential risk of deadlock which might have caused the ANR in the first place. Bug: 189840177, 189861955 Test: atest ShortcutManagerTest1 ShortcutManagerTest2 ShortcutManagerTest3 ShortcutManagerTest4 ShortcutManagerTest5 ShortcutManagerTest6 ShortcutManagerTest7 ShortcutManagerTest8 ShortcutManagerTest9 ShortcutManagerTest10 ShortcutManagerTest11 ShortcutManagerTest12 Test: atest ShortcutManagerClientApiTest Change-Id: I33fe05a08df43e926904de4420f863f65d3cbad8 --- .../android/server/pm/ShortcutBitmapSaver.java | 16 +++++++--------- .../com/android/server/pm/ShortcutService.java | 10 ---------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/services/core/java/com/android/server/pm/ShortcutBitmapSaver.java b/services/core/java/com/android/server/pm/ShortcutBitmapSaver.java index f411c98433cf7..901f96f7054ba 100644 --- a/services/core/java/com/android/server/pm/ShortcutBitmapSaver.java +++ b/services/core/java/com/android/server/pm/ShortcutBitmapSaver.java @@ -61,7 +61,7 @@ public class ShortcutBitmapSaver { * Before saving shortcuts.xml, and returning icons to the launcher, we wait for all pending * saves to finish. However if it takes more than this long, we just give up and proceed. */ - private final long SAVE_WAIT_TIMEOUT_MS = 30 * 1000; + private final long SAVE_WAIT_TIMEOUT_MS = 5 * 1000; private final ShortcutService mService; @@ -281,7 +281,7 @@ public class ShortcutBitmapSaver { } final String path = file.getAbsolutePath(); - mService.postValue(shortcut, si -> si.setBitmapPath(path)); + shortcut.setBitmapPath(path); } catch (IOException | RuntimeException e) { Slog.e(ShortcutService.TAG, "Unable to write bitmap to file", e); @@ -296,14 +296,12 @@ public class ShortcutBitmapSaver { Slog.d(TAG, "Saved bitmap."); } if (shortcut != null) { - mService.postValue(shortcut, si -> { - if (si.getBitmapPath() == null) { - removeIcon(si); - } + if (shortcut.getBitmapPath() == null) { + removeIcon(shortcut); + } - // Whatever happened, remove this flag. - si.clearFlags(ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE); - }); + // Whatever happened, remove this flag. + shortcut.clearFlags(ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE); } } return true; diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java index 1e9d7e1f8ad2d..5f10277972928 100644 --- a/services/core/java/com/android/server/pm/ShortcutService.java +++ b/services/core/java/com/android/server/pm/ShortcutService.java @@ -1208,16 +1208,6 @@ public class ShortcutService extends IShortcutService.Stub { } } - void postValue(@NonNull final ShortcutInfo shortcutInfo, - @NonNull final Consumer cb) { - final String pkg = shortcutInfo.getPackage(); - final int userId = shortcutInfo.getUserId(); - final String id = shortcutInfo.getId(); - synchronized (mLock) { - getPackageShortcutsLocked(pkg, userId).mutateShortcut(id, shortcutInfo, cb); - } - } - /** Return the last reset time. */ @GuardedBy("mLock") long getLastResetTimeLocked() {