Skip to content

Commit 0213e1e

Browse files
Fix race in credentialsProvider.setChangeListener (#919)
1 parent 1a3eb7c commit 0213e1e

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- [feature] Added an `addSnapshotsInSyncListener()` method to
1010
`FirebaseFirestore`that notifies you when all your snapshot listeners are
1111
in sync with each other.
12+
- [fixed] Fix a race condition that could cause a `NullPointerException` during
13+
client initialization.
1214

1315
# 21.1.2
1416
- [fixed] Fixed a crash that could occur when a large number of documents were

firebase-firestore/src/main/java/com/google/firebase/firestore/core/FirestoreClient.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,6 @@ public FirestoreClient(
9696

9797
TaskCompletionSource<User> firstUser = new TaskCompletionSource<>();
9898
final AtomicBoolean initialized = new AtomicBoolean(false);
99-
credentialsProvider.setChangeListener(
100-
(User user) -> {
101-
if (initialized.compareAndSet(false, true)) {
102-
hardAssert(!firstUser.getTask().isComplete(), "Already fulfilled first user task");
103-
firstUser.setResult(user);
104-
} else {
105-
asyncQueue.enqueueAndForget(
106-
() -> {
107-
Logger.debug(LOG_TAG, "Credential changed. Current user: %s", user.getUid());
108-
syncEngine.handleCredentialChange(user);
109-
});
110-
}
111-
});
11299

113100
// Defer initialization until we get the current user from the changeListener. This is
114101
// guaranteed to be synchronously dispatched onto our worker queue, so we will be initialized
@@ -127,6 +114,21 @@ public FirestoreClient(
127114
throw new RuntimeException(e);
128115
}
129116
});
117+
118+
credentialsProvider.setChangeListener(
119+
(User user) -> {
120+
if (initialized.compareAndSet(false, true)) {
121+
hardAssert(!firstUser.getTask().isComplete(), "Already fulfilled first user task");
122+
firstUser.setResult(user);
123+
} else {
124+
asyncQueue.enqueueAndForget(
125+
() -> {
126+
hardAssert(syncEngine != null, "SyncEngine not yet initialized");
127+
Logger.debug(LOG_TAG, "Credential changed. Current user: %s", user.getUid());
128+
syncEngine.handleCredentialChange(user);
129+
});
130+
}
131+
});
130132
}
131133

132134
public Task<Void> disableNetwork() {

0 commit comments

Comments
 (0)