From 01ee1bdec736d7d9ec05a5344c7e5638b12ae564 Mon Sep 17 00:00:00 2001 From: Greg Sakakihara Date: Tue, 15 Nov 2022 07:05:42 -0800 Subject: [PATCH] Fix deadlock when handling simultaneous messages. https://github.com/firebase/firebase-android-sdk/issues/4315 * Released sendWakefulServiceIntent()'s WakeLock on the main thread instead of within WithinAppServiceConnection to prevent a deadlock trying to acquire the WakeLockHolder.syncObject. --- firebase-messaging/CHANGELOG.md | 5 ++++- .../java/com/google/firebase/messaging/WakeLockHolder.java | 4 +--- .../google/firebase/messaging/WakeLockHolderRoboTest.java | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/firebase-messaging/CHANGELOG.md b/firebase-messaging/CHANGELOG.md index a22bf487c22..da7fef68b2e 100644 --- a/firebase-messaging/CHANGELOG.md +++ b/firebase-messaging/CHANGELOG.md @@ -1,10 +1,13 @@ # Unreleased +# 23.1.1 + +* [fixed] Fixed deadlock when handling simultaneous messages. + # 23.1.0 * [unchanged] Updated to accommodate the release of the updated [messaging_longer] Kotlin extensions library. - ## Kotlin The Kotlin extensions library transitively includes the updated `firebase-messaging` library. The Kotlin extensions library has the following diff --git a/firebase-messaging/src/main/java/com/google/firebase/messaging/WakeLockHolder.java b/firebase-messaging/src/main/java/com/google/firebase/messaging/WakeLockHolder.java index 84001d9f87d..8669390007b 100644 --- a/firebase-messaging/src/main/java/com/google/firebase/messaging/WakeLockHolder.java +++ b/firebase-messaging/src/main/java/com/google/firebase/messaging/WakeLockHolder.java @@ -108,9 +108,7 @@ static void sendWakefulServiceIntent( wakeLock.acquire(WAKE_LOCK_ACQUIRE_TIMEOUT_MILLIS); } - connection - .sendIntent(intent) - .addOnCompleteListener(Runnable::run, t -> completeWakefulIntent(intent)); + connection.sendIntent(intent).addOnCompleteListener(t -> completeWakefulIntent(intent)); } } diff --git a/firebase-messaging/src/test/java/com/google/firebase/messaging/WakeLockHolderRoboTest.java b/firebase-messaging/src/test/java/com/google/firebase/messaging/WakeLockHolderRoboTest.java index 60661fc6a3c..3600c6ee8fc 100644 --- a/firebase-messaging/src/test/java/com/google/firebase/messaging/WakeLockHolderRoboTest.java +++ b/firebase-messaging/src/test/java/com/google/firebase/messaging/WakeLockHolderRoboTest.java @@ -27,6 +27,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.robolectric.shadows.ShadowLooper; import org.robolectric.shadows.ShadowPowerManager; @RunWith(RobolectricTestRunner.class) @@ -77,6 +78,7 @@ public void testSendWakefulServiceIntent_ReleasesWakeLock() { WakeLock wakeLock = ShadowPowerManager.getLatestWakeLock(); taskCompletionSource.setResult(null); + ShadowLooper.idleMainLooper(); assertThat(wakeLock.isHeld()).isFalse(); }