Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- [fixed] Fix a race condition that could cause a segmentation fault during
client initialization.

# v1.6.1
- [changed] Internal improvements.

Expand Down
20 changes: 6 additions & 14 deletions Firestore/core/src/firebase/firestore/core/firestore_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,20 @@
new FirestoreClient(database_info, std::move(credentials_provider),
std::move(user_executor), std::move(worker_queue)));

auto user_promise = std::make_shared<std::promise<User>>();
bool credentials_initialized = false;

std::weak_ptr<FirestoreClient> weak_client(shared_client);
auto credential_change_listener = [credentials_initialized, user_promise,
weak_client](User user) mutable {
auto credential_change_listener = [credentials_initialized, weak_client,
settings](User user) mutable {
auto shared_client = weak_client.lock();
if (!shared_client) return;

if (!credentials_initialized) {
credentials_initialized = true;
user_promise->set_value(user);

shared_client->worker_queue()->Enqueue([shared_client, user, settings] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work.

The whole point of enqueueing outside the credential change listener is that we want to prevent any API call from getting on the async queue before we have the current identity.

The user_promise->get_future().get() blocks in the body of that callback essentially preventing the async queue from doing anything until we've gotten the callback.

With this change, any API call can now proceed before Auth has called us back and will hit the internals before we're initialized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, this is actually safe since SetCredentialChangeListener invokes the callback synchronously on the calling thread when it is first registered. Thus, the client initialization continues to be the first item enqueue on the worker queue.

I added a comment and an assert that makes this more obvious. As part of this, I also had to change credentials_initialized tp be captured by reference.

shared_client->Initialize(user, settings);
});
} else {
shared_client->worker_queue()->Enqueue([shared_client, user] {
shared_client->worker_queue()->VerifyIsCurrentQueue();
Expand All @@ -119,16 +121,6 @@ new FirestoreClient(database_info, std::move(credentials_provider),
shared_client->credentials_provider_->SetCredentialChangeListener(
credential_change_listener);

// Defer initialization until we get the current user from the
// credential_change_listener. This is guaranteed to be synchronously
// dispatched onto our worker queue, so we will be initialized before any
// subsequently queued work runs.
shared_client->worker_queue()->Enqueue(
[shared_client, user_promise, settings] {
User user = user_promise->get_future().get();
shared_client->Initialize(user, settings);
});

return shared_client;
}

Expand Down