-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix potential race during client initialization #4091
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,18 +94,24 @@ | |
| 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); | ||
|
|
||
| // When we register the credentials listener for the first time, | ||
| // it is invoked synchronously on the calling thread. This ensures that | ||
| // the first item enqueued on the worker queue is | ||
| // `FirestoreClient::Initialize()`. | ||
| shared_client->worker_queue()->Enqueue([shared_client, user, settings] { | ||
| shared_client->Initialize(user, settings); | ||
| }); | ||
| } else { | ||
| shared_client->worker_queue()->Enqueue([shared_client, user] { | ||
| shared_client->worker_queue()->VerifyIsCurrentQueue(); | ||
|
|
@@ -119,15 +125,9 @@ 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); | ||
| }); | ||
| HARD_ASSERT( | ||
| credentials_initialized, | ||
|
||
| "CredentialChangeListener not invoked during client initialization"); | ||
|
|
||
| return shared_client; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
SetCredentialChangeListenerinvokes 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.