Skip to content
Open
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
5 changes: 5 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,11 @@ code.
## Release Notes
### Upcoming
- Changes
- Remote Config: Fixed `ConfigInfo::throttled_end_time` being left
uninitialized in `RemoteConfig::EnsureInitialized()` on Android, added
default member initializers to `ConfigInfo`, captured throttle end times
from Android `FetchAndActivate()`, and clamped negative/out-of-range
timestamps across Android, iOS, and Desktop.
- Database (Desktop): Added exponential backoff to Realtime Database
reconnection attempts to prevent tight reconnect loops.
- General (Desktop): Fixed Auth and App Check staying registered with their
Expand Down
19 changes: 19 additions & 0 deletions remote_config/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,25 @@ TEST_F(FirebaseRemoteConfigTest, TestFetchSecondsParameter) {
FLAKY_TEST_SECTION_END();
}

TEST_F(FirebaseRemoteConfigTest, TestEnsureInitialized) {
ASSERT_NE(rc_, nullptr);

Future<firebase::remote_config::ConfigInfo> future = rc_->EnsureInitialized();
EXPECT_TRUE(WaitForCompletion(future, "EnsureInitialized"));
EXPECT_EQ(rc_->EnsureInitializedLastResult().error(), 0);
ASSERT_NE(future.result(), nullptr);

// Max milliseconds past Unix epoch representable by .NET DateTime
// (9999-12-31T23:59:59.999Z); uninitialized stack values typically exceed
// this bound.
constexpr uint64_t kMaxValidEpochMillis = 253402300799999ULL;
const firebase::remote_config::ConfigInfo& info = *future.result();
const firebase::remote_config::ConfigInfo current_info = rc_->GetInfo();
EXPECT_LE(info.fetch_time, kMaxValidEpochMillis);
EXPECT_LE(info.throttled_end_time, kMaxValidEpochMillis);
EXPECT_EQ(info.throttled_end_time, current_info.throttled_end_time);
}

TEST_F(FirebaseRemoteConfigTest, TestSetCustomSignals) {
ASSERT_NE(rc_, nullptr);

Expand Down
99 changes: 68 additions & 31 deletions remote_config/src/android/remote_config_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,31 @@ static RemoteConfigError RemoteConfigErrorFromJavaErrorCode(int error_code) {
}

static void JConfigInfoToConfigInfo(JNIEnv* env, jobject jinfo,
uint64_t throttled_end_time,
ConfigInfo* info) {
FIREBASE_DEV_ASSERT(info != nullptr);
info->throttled_end_time = throttled_end_time;
if (!jinfo) {
return;
}
FIREBASE_DEV_ASSERT(env->IsInstanceOf(jinfo, config_info::GetClass()));

int64_t fetch_time = env->CallLongMethod(
jinfo, config_info::GetMethodId(config_info::kGetFetchTimeMillis));
// The C++ fetch_time is a uint64_t, so if we are given a negative number,
// use 0 instead, to prevent getting a very large number.
if (fetch_time < 0) {
// The C++ fetch_time is a uint64_t, so if we are given a negative number
// (e.g. -1 before any fetch) or a JNI exception occurs, use 0 instead to
// prevent wrapping to a very large number.
if (util::CheckAndClearJniExceptions(env) || fetch_time < 0) {
fetch_time = 0;
}
info->fetch_time = fetch_time;
util::CheckAndClearJniExceptions(env);
int64_t status_code = env->CallIntMethod(
info->fetch_time = static_cast<uint64_t>(fetch_time);
int32_t status_code = env->CallIntMethod(
jinfo, config_info::GetMethodId(config_info::kGetLastFetchStatus));
if (util::CheckAndClearJniExceptions(env)) {
info->last_fetch_status = kLastFetchStatusFailure;
info->last_fetch_failure_reason = kFetchFailureReasonError;
return;
}
switch (status_code) {
case -1: // FirebaseRemoteConfig.LAST_FETCH_STATUS_SUCCESS
info->last_fetch_status = kLastFetchStatusSuccess;
Expand All @@ -643,7 +654,6 @@ static void JConfigInfoToConfigInfo(JNIEnv* env, jobject jinfo,
info->last_fetch_failure_reason = kFetchFailureReasonInvalid;
break;
}
util::CheckAndClearJniExceptions(env);
}

template <typename T>
Expand Down Expand Up @@ -722,34 +732,64 @@ RemoteConfigInternal::~RemoteConfigInternal() {
// ConfigUpdateListenerRegistration instances to no longer point to the
// corresponding internal objects.
cleanup_notifier().CleanupAll();
JNIEnv* env = app_.GetJNIEnv();
util::CancelCallbacks(env, jni_task_id_.c_str());
if (!callbacks_cancelled_) {
callbacks_cancelled_ = true;
JNIEnv* env = app_.GetJNIEnv();
util::CancelCallbacks(env, jni_task_id_.c_str());
}
}

bool RemoteConfigInternal::Initialized() const {
return internal_obj_ != nullptr;
}

void RemoteConfigInternal::Cleanup() {
JNIEnv* env = app_.GetJNIEnv();
// Cancel pending JNI task callbacks before releasing cached JNI classes or
// terminating util, and before acquiring ReferenceCountLock to avoid lock
// inversion with completion callbacks.
if (!callbacks_cancelled_) {
callbacks_cancelled_ = true;
util::CancelCallbacks(env, jni_task_id_.c_str());
}
ReferenceCountLock<ReferenceCount> lock(&initializer_);
if (lock.RemoveReference() == 1) {
JNIEnv* env = app_.GetJNIEnv();
ReleaseClasses(env);
util::Terminate(env);
}
}

static void UpdateThrottledEndTimeFromResult(
JNIEnv* env, jobject result, util::FutureResult result_code,
RemoteConfigInternal* rc_internal) {
jclass throttled_class = throttled_exception::GetClass();
if (result_code != util::kFutureResultSuccess && result && rc_internal &&
throttled_class && env->IsInstanceOf(result, throttled_class)) {
int64_t throttle_end_time = static_cast<int64_t>(env->CallLongMethod(
result, throttled_exception::GetMethodId(
throttled_exception::kGetThrottleEndTimeMillis)));
if (!util::CheckAndClearJniExceptions(env) && throttle_end_time > 0) {
rc_internal->set_throttled_end_time(throttle_end_time);
}
}
}

void EnsureInitializedCallback(JNIEnv* env, jobject result,
util::FutureResult result_code,
const char* status_message,
void* callback_data) {
auto* data_handle =
reinterpret_cast<RCDataHandle<ConfigInfo>*>(callback_data);
bool success = (result_code == util::kFutureResultSuccess);
uint64_t throttled_end_time =
data_handle->rc_internal ? data_handle->rc_internal->throttled_end_time()
: 0;
ConfigInfo info;
if (success && result) {
JConfigInfoToConfigInfo(env, result, &info);
JConfigInfoToConfigInfo(env, result, throttled_end_time, &info);
} else {
info.throttled_end_time = throttled_end_time;
}
auto* data_handle =
reinterpret_cast<RCDataHandle<ConfigInfo>*>(callback_data);

data_handle->future_api->CompleteWithResult(
data_handle->future_handle,
Expand Down Expand Up @@ -777,6 +817,15 @@ void BoolResultCallback(JNIEnv* env, jobject result,
delete data_handle;
}

void FetchAndActivateCallback(JNIEnv* env, jobject result,
util::FutureResult result_code,
const char* status_message, void* callback_data) {
auto* data_handle = reinterpret_cast<RCDataHandle<bool>*>(callback_data);
UpdateThrottledEndTimeFromResult(env, result, result_code,
data_handle->rc_internal);
BoolResultCallback(env, result, result_code, status_message, callback_data);
}

void CompleteVoidCallback(JNIEnv* env, jobject result,
util::FutureResult result_code,
const char* status_message, void* callback_data) {
Expand All @@ -790,20 +839,9 @@ void CompleteVoidCallback(JNIEnv* env, jobject result,

void FetchCallback(JNIEnv* env, jobject result, util::FutureResult result_code,
const char* status_message, void* callback_data) {
bool success = (result_code == util::kFutureResultSuccess);
int64_t throttle_end_time = 0;
if (!success && result) {
if (env->IsInstanceOf(result, throttled_exception::GetClass())) {
throttle_end_time = static_cast<int64_t>(env->CallLongMethod(
result, throttled_exception::GetMethodId(
throttled_exception::kGetThrottleEndTimeMillis)));
}
}
auto data_handle = reinterpret_cast<RCDataHandle<void>*>(callback_data);
if (throttle_end_time > 0) {
data_handle->rc_internal->set_throttled_end_time(throttle_end_time);
}

auto* data_handle = reinterpret_cast<RCDataHandle<void>*>(callback_data);
UpdateThrottledEndTimeFromResult(env, result, result_code,
data_handle->rc_internal);
CompleteVoidCallback(env, result, result_code, status_message, callback_data);
}

Expand Down Expand Up @@ -882,7 +920,7 @@ Future<bool> RemoteConfigInternal::FetchAndActivate() {
"FetchAndActivate native function fails");
} else {
auto data_handle = new RCDataHandle<bool>(&future_impl_, handle, this);
util::RegisterCallbackOnTask(env, task, BoolResultCallback,
util::RegisterCallbackOnTask(env, task, FetchAndActivateCallback,
reinterpret_cast<void*>(data_handle),
jni_task_id_.c_str());
}
Expand Down Expand Up @@ -1353,13 +1391,12 @@ std::map<std::string, Variant> RemoteConfigInternal::GetAll() {
const ConfigInfo RemoteConfigInternal::GetInfo() const {
JNIEnv* env = app_.GetJNIEnv();
ConfigInfo info;
info.throttled_end_time = throttled_end_time_;

jobject jinfo = env->CallObjectMethod(internal_obj_,
config::GetMethodId(config::kGetInfo));
if (util::CheckAndClearJniExceptions(env)) jinfo = nullptr;
JConfigInfoToConfigInfo(env, jinfo, &info);
env->DeleteLocalRef(jinfo);
JConfigInfoToConfigInfo(env, jinfo, throttled_end_time(), &info);
if (jinfo) env->DeleteLocalRef(jinfo);
return info;
}

Expand Down
13 changes: 11 additions & 2 deletions remote_config/src/android/remote_config_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdint.h>

#include <atomic>
#include <string>
#include <utility>

Expand Down Expand Up @@ -99,7 +100,13 @@ class RemoteConfigInternal {
CleanupNotifier& cleanup_notifier() { return cleanup_; }

void set_throttled_end_time(int64_t end_time) {
throttled_end_time_ = end_time;
throttled_end_time_.store(
end_time > 0 ? static_cast<uint64_t>(end_time) : 0,
std::memory_order_relaxed);
}

uint64_t throttled_end_time() const {
return throttled_end_time_.load(std::memory_order_relaxed);
}

void SaveTmpKeysToDefault(std::vector<std::string> tmp_default_keys) {
Expand All @@ -125,10 +132,12 @@ class RemoteConfigInternal {

// If a fetch was throttled, this is set to the time when throttling is
// finished, in milliseconds since epoch.
int64_t throttled_end_time_ = 0;
std::atomic<uint64_t> throttled_end_time_{0};

// String to be used when registering for JNI task callbacks.
std::string jni_task_id_;

bool callbacks_cancelled_ = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since callbacks_cancelled_ is accessed and modified outside of the ReferenceCountLock in Cleanup() and can potentially be accessed concurrently or across different threads during cleanup/destruction, it should be declared as std::atomic<bool> to prevent data races and undefined behavior.

Suggested change
bool callbacks_cancelled_ = false;
std::atomic<bool> callbacks_cancelled_{false};

};

} // namespace internal
Expand Down
7 changes: 4 additions & 3 deletions remote_config/src/desktop/rest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ void RemoteConfigREST::FetchFailure(FetchFailureReason reason) {
}

uint64_t RemoteConfigREST::MillisecondsSinceEpoch() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
return ms > 0 ? static_cast<uint64_t>(ms) : 0;
}

} // namespace internal
Expand Down
8 changes: 4 additions & 4 deletions remote_config/src/include/firebase/remote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ struct ConfigUpdate {
struct ConfigInfo {
/// @brief The time (in milliseconds since the epoch) that the last fetch
/// operation completed. 0 if no fetch attempt has been made yet.
uint64_t fetch_time;
uint64_t fetch_time = 0;

/// @brief The status of the last fetch request.
LastFetchStatus last_fetch_status;
LastFetchStatus last_fetch_status = kLastFetchStatusPending;

/// @brief The reason the most recent fetch failed.
FetchFailureReason last_fetch_failure_reason;
FetchFailureReason last_fetch_failure_reason = kFetchFailureReasonInvalid;

/// @brief The time (in milliseconds since the epoch) when the refreshing of
/// Remote Config data is throttled.
uint64_t throttled_end_time;
uint64_t throttled_end_time = 0;
};

/// @brief Describes the source a config value was retrieved from.
Expand Down
3 changes: 2 additions & 1 deletion remote_config/src/ios/remote_config_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define FIREBASE_REMOTE_CONFIG_SRC_IOS_REMOTE_CONFIG_IOS_H_

#include <stdint.h>
#include <atomic>
#include <functional>
#include <memory>
#include <string>
Expand Down Expand Up @@ -122,7 +123,7 @@ class RemoteConfigInternal {
// Saved default keys.
std::vector<std::string> default_keys_;

int64_t throttled_end_time_in_sec_;
std::atomic<int64_t> throttled_end_time_in_sec_{0};
};
} // namespace internal
} // namespace remote_config
Expand Down
Loading
Loading