Skip to content

Commit c7ffa8c

Browse files
authored
Firestore: Make ListenerRegistration::Remove() thread-safe (#10065)
1 parent 782f083 commit c7ffa8c

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [fixed] Fixed an intermittent crash if `ListenerRegistration::Remove()` was
3+
invoked concurrently (#10065).
4+
15
# 9.4.0
26
- [fixed] Fixed a crash during app start (#9985, #10018).
37

Firestore/core/src/api/query_listener_registration.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Firestore/core/src/core/firestore_client.h"
2323
#include "Firestore/core/src/core/query_listener.h"
2424
#include "Firestore/core/src/core/view_snapshot.h"
25+
#include "absl/synchronization/mutex.h"
2526

2627
namespace firebase {
2728
namespace firestore {
@@ -38,6 +39,8 @@ QueryListenerRegistration::QueryListenerRegistration(
3839
}
3940

4041
void QueryListenerRegistration::Remove() {
42+
absl::MutexLock lock(&mutex_);
43+
4144
auto async_listener = async_listener_.lock();
4245
if (async_listener) {
4346
async_listener->Mute();

Firestore/core/src/api/query_listener_registration.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "Firestore/core/src/api/listener_registration.h"
2323
#include "Firestore/core/src/core/core_fwd.h"
24+
#include "absl/base/thread_annotations.h"
25+
#include "absl/synchronization/mutex.h"
2426

2527
namespace firebase {
2628
namespace firestore {
@@ -44,14 +46,17 @@ class QueryListenerRegistration : public ListenerRegistration {
4446
void Remove() override;
4547

4648
private:
49+
absl::Mutex mutex_;
50+
4751
/** The client that was used to register this listen. */
48-
std::shared_ptr<core::FirestoreClient> client_;
52+
std::shared_ptr<core::FirestoreClient> client_ ABSL_GUARDED_BY(mutex_);
4953

5054
/** The async listener that is used to mute events synchronously. */
51-
std::weak_ptr<core::AsyncEventListener<core::ViewSnapshot>> async_listener_;
55+
std::weak_ptr<core::AsyncEventListener<core::ViewSnapshot>> async_listener_
56+
ABSL_GUARDED_BY(mutex_);
5257

5358
/** The internal QueryListener that can be used to unlisten the query. */
54-
std::weak_ptr<core::QueryListener> query_listener_;
59+
std::weak_ptr<core::QueryListener> query_listener_ ABSL_GUARDED_BY(mutex_);
5560
};
5661

5762
} // namespace api

0 commit comments

Comments
 (0)