Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore: Make ListenerRegistration::Remove() thread-safe #10065

Merged
merged 5 commits into from
Aug 4, 2022

Conversation

dconeybe
Copy link
Contributor

@dconeybe dconeybe commented Aug 3, 2022

This fixes a latent bug in Firestore's ListenerRegistration::Remove() where it could use invalid memory if invoked concurrently. The fix is to simply guard the entire method's body with a mutex.

The issue is that one thread could use client_ (a shared_ptr) that has been reset by another thread. Specifically, in this code block

auto query_listener = query_listener_.lock();
if (query_listener) {
client_->RemoveListener(query_listener);
query_listener_.reset();
}
client_.reset();

one thread could invoke client_.reset() on line 53 just before another thread invokes client_->RemoveListener(query_listener) on line 49.

This may be the root cause of #10026 and #9302. I'll ask the customer to report back once they upgrade to a version that contains this fix.

@dconeybe dconeybe self-assigned this Aug 3, 2022
@dconeybe dconeybe changed the base branch from master to dconeybe/RemoveFirestoreAbslCheck August 3, 2022 19:29
@dconeybe dconeybe requested a review from ehsannas August 3, 2022 20:27
@google-oss-bot
Copy link

google-oss-bot commented Aug 3, 2022

Coverage Report 1

Affected Products

  • FirebaseFirestore-iOS-FirebaseFirestore.framework

    Overall coverage changed from 88.59% (26075af) to 88.57% (d329277) by -0.02%.

    FilenameBase (26075af)Merge (d329277)Diff
    leveldb_key.cc98.43%98.14%-0.29%
    ordered_code.cc94.39%93.90%-0.49%

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/tLl6QzFKEf.html

Base automatically changed from dconeybe/RemoveFirestoreAbslCheck to master August 4, 2022 20:40
@dconeybe dconeybe merged commit c7ffa8c into master Aug 4, 2022
@dconeybe dconeybe deleted the dconeybe/ListenerRegistrationRemoveConcurrencyFix branch August 4, 2022 21:53
@firebase firebase locked and limited conversation to collaborators Sep 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants