Skip to content

[Remote Config] Initialize ConfigInfo fields and fix Android EnsureInitialized throttled_end_time - #1998

Open
AustinBenoit wants to merge 4 commits into
mainfrom
fix/remote-config-configinfo-init
Open

AustinBenoit wants to merge 4 commits into
mainfrom
fix/remote-config-configinfo-init

Conversation

@AustinBenoit

Copy link
Copy Markdown
Contributor

Description

Fixes #1997 (and the underlying root cause of firebase/firebase-unity-sdk#1481).

In remote_config/src/android/remote_config_android.cc, EnsureInitializedCallback() declared ConfigInfo info; uninitialized and passed it to JConfigInfoToConfigInfo(), which never set info->throttled_end_time (unlike RemoteConfigInternal::GetInfo()). Because struct ConfigInfo had no default member initializers, EnsureInitialized() returned stack garbage for throttled_end_time on Android—causing intermittent System.ArgumentOutOfRangeException in Unity's ConfigInfo constructor whenever stale stack bytes exceeded DateTime.MaxValue.

Summary of changes:

  • Public header (remote_config/src/include/firebase/remote_config.h): Default-initialize all fields on struct ConfigInfo (fetch_time = 0, last_fetch_status = kLastFetchStatusPending, last_fetch_failure_reason = kFetchFailureReasonInvalid, throttled_end_time = 0).
  • Android (remote_config/src/android/remote_config_android.{h,cc}):
    • Thread throttled_end_time into JConfigInfoToConfigInfo() and populate it in EnsureInitializedCallback() (both success and failure paths).
    • Guard against nullptr jinfo in GetInfo() / JConfigInfoToConfigInfo() and check JNI exceptions before converting fetch_time / status_code.
    • Capture throttle end times on FetchAndActivate() via FetchAndActivateCallback + UpdateThrottledEndTimeFromResult() (matching Fetch() and iOS).
    • Store throttled_end_time_ in std::atomic<uint64_t> and drain util::CancelCallbacks() at the start of Cleanup() before ReleaseClasses() / util::Terminate().
  • iOS (remote_config/src/ios/remote_config_ios.{h,mm}):
    • Store throttled_end_time_in_sec_ in std::atomic<int64_t> strictly before completing futures, only updating on positive NSNumber throttle timestamps (preserving throttle state on non-throttled errors and populating config_info.throttled_end_time on EnsureInitialized() error).
    • Clamp negative/non-finite fetch_time and guard against overflow when converting seconds to milliseconds.
  • Desktop (remote_config/src/desktop/rest.cc): Clamp negative MillisecondsSinceEpoch() to 0.
  • Tests & release notes: Added unit tests in metadata_test.cc and remote_config_desktop_test.cc, added TestEnsureInitialized in remote_config/integration_test/src/integration_test.cc, and updated release_build_files/readme.md.

Testing

  • C++ Desktop Debug and ASan+UBSan (ctest --output-on-failure -R remote_config): 4/4 targets, 50/50 gtest cases passed, 0 sanitizer errors, 0 warnings on modified lines.
  • Android NDK 23.2.8568313 (r23c) and 28.2.13676358 (r28b), aarch64 & armv7a (remote_config_android.cc and remote_config.cc with -std=c++17 -DINTERNAL_EXPERIMENTAL=1 -Wall -Wextra): compiled cleanly with 0 errors and 0 warnings on modified lines.
  • remote_config/integration_test/src/integration_test.cc: compiled cleanly with g++ -std=c++17 -Wall -Wextra.
  • Paired Unity SWIG (SWIG 4.4.1) + Firebase.RemoteConfig.dll build against this branch: 0 SWIGTYPE_p_ leaks, 0 compiler warnings.
  • Verified python3 scripts/format_code.py --git_diff --noformat_file and ./scripts/gha/check_copyright.sh -g.

Type of Change

Place an x in the applicable box:

…itialized throttled_end_time (#1997)

Fixes #1997.

- Default-initialize all fields of ConfigInfo in remote_config.h.
- Thread throttled_end_time into JConfigInfoToConfigInfo and populate it in Android EnsureInitializedCallback (and guard against null jinfo and JNI exceptions).
- Capture throttle end times on Android FetchAndActivate() via FetchAndActivateCallback, store throttled_end_time_ atomically, and drain JNI task callbacks in Cleanup() before ReleaseClasses()/util::Terminate().
- On iOS, store throttled_end_time_in_sec_ atomically strictly before completing futures, only update on positive NSNumber throttle timestamps, clamp negative/non-finite fetch_time, and prevent overflow when converting seconds to milliseconds.
- On Desktop, clamp negative MillisecondsSinceEpoch() to 0.
- Add unit tests (metadata_test.cc, remote_config_desktop_test.cc), integration test (integration_test.cc), and Upcoming release note.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request addresses several issues in Remote Config across Android, iOS, and Desktop platforms. Key changes include adding default member initializers to ConfigInfo, capturing throttle end times from Android's FetchAndActivate, and clamping negative or out-of-range timestamps to prevent wrapping. Additionally, throttled_end_time is now managed using atomic variables to ensure thread safety. The reviewer recommends declaring callbacks_cancelled_ as std::atomic<bool> in remote_config_android.h to prevent potential data races during cleanup and destruction.

// 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};

- Revert 4 unrelated clang-format hunks in remote_config_desktop_test.cc (fixes CI file_format_check).
- Poison storage with 0xAA + placement-new default-init in ConfigInfoDefaultInitialized test for mutation resistance.
- Guard non-finite/out-of-range NSNumber doubles in iOS UpdateThrottledEndTimeFromError.
- Clarify Cleanup() comment ('Cancel' instead of 'Drain') to match util::CancelCallbacks semantics.
AustinBenoit added a commit to firebase/firebase-unity-sdk that referenced this pull request Nov 27, 2026
…st and static readonly UnixEpochUtc

Let firebase/firebase-cpp-sdk#1998 fix the underlying Android uninitialized
ConfigInfo::throttled_end_time bug at the root rather than clamping in C#,
keeping ConfigInfo consistent with StorageMetadata, AppCheckToken,
UserMetadata, and IdTokenResult.
// 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;

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.

I don't think this check makes a lot of sense (also, reference to .NET doesn't make sense here). Just the last check of info and current_info should be fine, since that would catch if they aren't initializing as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — removed the max-epoch check and .NET comment, keeping only the info vs. current_info comparison.

AustinBenoit added a commit to firebase/firebase-unity-sdk that referenced this pull request Dec 2, 2026
…nfo.UnixEpochUtc static readonly (#1581)

* [Remote Config] Clamp out-of-range ConfigInfo timestamps to DateTime.MaxValue (UTC)

Paired with firebase/firebase-cpp-sdk#1997 (and #1481).

- Replace UnixEpochUtc.AddMilliseconds(ulong) in ConfigInfo.cs with tick-exact FromUnixEpochMilliseconds(ulong) clamping to DateTime.MaxValue (UTC), preventing ArgumentOutOfRangeException and double rounding overflow at the DateTime.MaxValue boundary.
- Add TestEnsureInitialized to Remote Config UIHandlerAutomated.cs verifying EnsureInitializedAsync() and Info return non-null UTC timestamps.
- Add release note under ### Upcoming Release in docs/readme.md.

* [Remote Config] Use UnixEpochUtc.AddMilliseconds under clamp guard

Match the repository's existing UnixEpochUtc.AddMilliseconds convention
(StorageMetadata, AppCheckToken, UserMetadata) while keeping the
MaxUnixEpochMilliseconds -> MaxDateTimeUtc guard. Every integer in
[0, 253402300799999] is exact in IEEE-754 double (< 2^53).

* [Remote Config] Drop C# timestamp clamping; keep EnsureInitialized test and static readonly UnixEpochUtc

Let firebase/firebase-cpp-sdk#1998 fix the underlying Android uninitialized
ConfigInfo::throttled_end_time bug at the root rather than clamping in C#,
keeping ConfigInfo consistent with StorageMetadata, AppCheckToken,
UserMetadata, and IdTokenResult.
@AustinBenoit AustinBenoit added the tests-requested: full Trigger a FULL set of integration tests (uses expanded test matrix). label Dec 2, 2026
@AustinBenoit
AustinBenoit requested review from a-maurice and removed request for a-maurice December 2, 2026 00:34
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests-requested: full Trigger a FULL set of integration tests (uses expanded test matrix). labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Integration test with FLAKINESS (succeeded after retry)

Requested by @AustinBenoit on commit 7c352d6
Last updated: Tue Dec 1 18:35 PST 2026
View integration test log & download artifacts

Failures Configs
firestore [TEST] [FLAKINESS] [Android] [1/3 os: macos] [1/4 android_device: emulator_ftl_latest]
(1 failed tests)  TaskTest.IsCompleteShouldReturnTrueForCanceledTask
[TEST] [FLAKINESS] [Android] [1/3 os: windows] [1/4 android_device: android_latest]
(1 failed tests)  ServerTimestampTest.TestServerTimestampsCanReturnPreviousValueOfDifferentType

Add flaky tests to go/fpl-cpp-flake-tracker

@github-actions github-actions Bot added the tests: succeeded This PR's integration tests succeeded. label Dec 2, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests: succeeded This PR's integration tests succeeded.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

throttled_end_time in ConfigInfo uninitialized

2 participants