Skip to content

Add null checks in Messaging task completions #1602

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

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 21 additions & 5 deletions messaging/src/android/cpp/messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ static std::string* g_lockfile_path;

static Mutex* g_file_locker_mutex;

// Mutex for managing if FutureData is safe to use, or if it has been deleted.
static Mutex g_future_data_mutex;

static const char* kMessagingNotInitializedError = "Messaging not initialized.";

static void HandlePendingSubscriptions();
Expand Down Expand Up @@ -706,7 +709,10 @@ void Terminate() {
SetListener(nullptr);
ReleaseClasses(env);
util::Terminate(env);
FutureData::Destroy();
{
MutexLock lock(g_future_data_mutex);
FutureData::Destroy();
}
}

// Start a service which will communicate with the Firebase Cloud Messaging
Expand Down Expand Up @@ -826,8 +832,13 @@ static void CompleteVoidCallback(JNIEnv* env, jobject result,
FutureHandle handle(future_id);
Error error =
(result_code == util::kFutureResultSuccess) ? kErrorNone : kErrorUnknown;
ReferenceCountedFutureImpl* api = FutureData::Get()->api();
api->Complete(handle, error, status_message);
MutexLock lock(g_future_data_mutex);
if (FutureData::Get() && FutureData::Get()->api()) {
ReferenceCountedFutureImpl* api = FutureData::Get()->api();
api->Complete(handle, error, status_message);
} else {
LogWarning("Failed to complete Future as it was likely already deleted.");
}
if (result) env->DeleteLocalRef(result);
}

Expand All @@ -843,8 +854,13 @@ static void CompleteStringCallback(JNIEnv* env, jobject result,
SafeFutureHandle<std::string>* handle =
reinterpret_cast<SafeFutureHandle<std::string>*>(callback_data);
Error error = success ? kErrorNone : kErrorUnknown;
ReferenceCountedFutureImpl* api = FutureData::Get()->api();
api->CompleteWithResult(*handle, error, status_message, result_value);
MutexLock lock(g_future_data_mutex);
if (FutureData::Get() && FutureData::Get()->api()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need a mutex lock?

ReferenceCountedFutureImpl* api = FutureData::Get()->api();
api->CompleteWithResult(*handle, error, status_message, result_value);
} else {
LogWarning("Failed to complete Future as it was likely already deleted.");
}
delete handle;
}

Expand Down
6 changes: 4 additions & 2 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,10 @@ code.
### Upcoming Release
- Changes
- Analytics (iOS): Add support for
`InitiateOnDeviceConversionMeasurementWithHashedEmailAddress` and
`InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber`.
`InitiateOnDeviceConversionMeasurementWithHashedEmailAddress` and
`InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber`.
- Messaging (Android): Addressed potential race condition on receiving
messages after cleanup.

### 12.0.0
- Changes
Expand Down
Loading