Skip to content

Commit 8f8718b

Browse files
authored
Fix for a deadlock in JniResultCallback (#1330)
1 parent 35f554b commit 8f8718b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

app/src_java/com/google/firebase/app/internal/cpp/JniResultCallback.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ private interface Callback {
4545

4646
public static final String TAG = "FirebaseCb";
4747

48+
// Object used to synchronize around. All synchronization should be done with this,
49+
// as there isn't a guarantee on which function will be called first.
50+
private final Object lockObject = new Object();
51+
4852
/**
4953
* This class is registered with a Task object as an OnSuccessListener, OnFailureListener, and
5054
* OnCanceledListener, redirecting completion status to the C++ method nativeOnResult.
5155
*/
5256
private class TaskCallback<TResult>
5357
implements OnSuccessListener<TResult>, OnFailureListener, OnCanceledListener, Callback {
5458
private Task<TResult> task;
55-
private final Object lockObject = new Object();
5659
/**
5760
* Register with a Task instance to capture the completion callback.
5861
*/
@@ -129,7 +132,7 @@ protected void initializeNativeCallbackFunctionAndData(long callbackFn, long cal
129132

130133
/** Initialize / attach the instance to a pending result or task object. */
131134
protected void initializeWithTask(Task<TResult> task) {
132-
synchronized (this) {
135+
synchronized (lockObject) {
133136
callbackHandler = new TaskCallback<TResult>(task);
134137
callbackHandler.register();
135138
}
@@ -146,7 +149,7 @@ public void cancel() {
146149
/** Call nativeOnResult with the registered callbackFn and callbackData. */
147150
public void onCompletion(
148151
Object result, boolean success, boolean cancelled, String statusMessage) {
149-
synchronized (this) {
152+
synchronized (lockObject) {
150153
if (callbackHandler != null) {
151154
nativeOnResult(result, success, cancelled, statusMessage, callbackFn, callbackData);
152155
callbackHandler.disconnect();

release_build_files/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ code.
629629
## Release Notes
630630
### Upcoming Release
631631
- Changes
632+
- General (Android): Fix for deadlock within JniResultCallback, commonly
633+
seen within Messaging, but affecting other products as well.
632634
- Database/Firestore (Desktop): Fixed a crash on Windows when the user's
633635
home directory contains non-ANSI characters (Unicode above U+00FF).
634636
- Storage (Desktop): Fixed a crash on Windows when uploading files from a

0 commit comments

Comments
 (0)