Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ebac819
Make per-App instance registries safe during static destruction
AustinBenoit Oct 26, 2026
61dcafb
Use function-local statics for the instance registries
AustinBenoit Oct 26, 2026
1d3b1f7
Add NoDestructor and use it for the instance registries
AustinBenoit Oct 26, 2026
e17d554
Merge branch 'main' into fix/static-destruction-order
AustinBenoit Oct 29, 2026
67a4892
Address review feedback on NoDestructor casts and nested locking
AustinBenoit Oct 29, 2026
f0bad88
Eliminate exit-time destructors and SIOF hazards across App and SDK m…
AustinBenoit Oct 29, 2026
7d6b83b
Merge branch 'main' into fix/static-destruction-order
AustinBenoit Oct 31, 2026
5a03fac
Merge branch 'main' into fix/static-destruction-order
AustinBenoit Nov 16, 2026
cabc7c3
Add Installations behavior change to the release notes
AustinBenoit Nov 16, 2026
9572f59
B2, m2: Constrain the NoDestructor forwarding constructor
AustinBenoit Nov 16, 2026
7f2bdd7
B2, m1: Test the constructor constraint; keep NoDestructor tests leak…
AustinBenoit Nov 16, 2026
891e576
B1, M4: Replace 13 copies of the exit-time destructor flag with a helper
AustinBenoit Nov 16, 2026
ae4ba7f
M4: Enforce the exit-time destructor check in CI
AustinBenoit Nov 16, 2026
c0fc3ac
m3: Test RemoveApp() for an App that is not registered
AustinBenoit Nov 16, 2026
5d8265d
n1: Correct the AppCallback::AddCallback comment
AustinBenoit Nov 16, 2026
a4151f6
Static destruction test: release the Future handle before its API
AustinBenoit Nov 16, 2026
7623588
Make the curl global mutex a function-local NoDestructor
AustinBenoit Nov 16, 2026
9a56360
m1: Make the std::string NoDestructor tests leak-free too
AustinBenoit Nov 16, 2026
3424dd6
m-F1: release the Android class reference when getInstance() returns …
AustinBenoit Nov 16, 2026
989892d
M3: add post-main() regression tests for static-owned product instances
AustinBenoit Nov 16, 2026
c9fb449
m-F2: test the failed-initialization, App-first and Auth re-creation …
AustinBenoit Nov 16, 2026
76f4803
Remove an unused <iostream> include from the Linux secure storage
AustinBenoit Nov 16, 2026
d7f01ae
Remove unused <iostream> includes from Database and Storage sources
AustinBenoit Nov 16, 2026
7ca15c8
Remove noisy pointer-alignment, rename, and already-heap-allocated diffs
AustinBenoit Nov 24, 2026
d6670dc
Include <vector> directly in reference_counted_future_impl.cc
AustinBenoit Nov 24, 2026
d7e4643
Merge remote-tracking branch 'origin/main' into fix/static-destructio…
AustinBenoit Nov 24, 2026
64da4d3
Guard Android CancelCallbacks in failed-init destructors
AustinBenoit Nov 24, 2026
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
2 changes: 2 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
env:
CCACHE_DIR: ${{ github.workspace }}/ccache_dir
GITHUB_TOKEN: ${{ github.token }}
# Fail Clang builds on SDK statics that need an exit-time destructor.
FIREBASE_CPP_WERROR_EXIT_TIME_DESTRUCTORS: 1
xcodeVersion: "26.4" # Only affects Mac runners, and only for prerequisites.

permissions:
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
env:
CCACHE_DIR: ${{ github.workspace }}/ccache_dir
GITHUB_TOKEN: ${{ github.token }}
# Fail Clang builds on SDK statics that need an exit-time destructor.
FIREBASE_CPP_WERROR_EXIT_TIME_DESTRUCTORS: 1

permissions:
contents: read
Expand Down Expand Up @@ -228,6 +230,13 @@ jobs:
find build -name "*.a"
find build -name "*.so"

# Linux builds use GCC, which has no -Wexit-time-destructors.
- name: Check firebase libraries for exit-time destructors (linux)
if: startsWith(matrix.os, 'ubuntu')
shell: bash
run: |
python scripts/gha/check_exit_time_destructors.py build/

- name: Inspect firebase libraries for cpu arch and msvc runtime.
shell: bash
run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ on:

env:
GITHUB_TOKEN: ${{ github.token }}
# Fail Clang builds on SDK statics that need an exit-time destructor.
FIREBASE_CPP_WERROR_EXIT_TIME_DESTRUCTORS: 1

permissions:
contents: read
Expand Down
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ option(FIREBASE_USE_LINUX_CXX11_ABI
option(FIREBASE_GITHUB_ACTION_BUILD
"Indicates that this build was created from a GitHub Action" OFF)

# Defaults to the environment variable of the same name, which the CI
# workflows set, so the check reaches CMake however it is launched (build
# scripts, Gradle, Xcode). See cmake/exit_time_destructors.cmake.
set(firebase_werror_exit_time_destructors_default OFF)
if("$ENV{FIREBASE_CPP_WERROR_EXIT_TIME_DESTRUCTORS}")
set(firebase_werror_exit_time_destructors_default ON)
endif()
option(FIREBASE_CPP_WERROR_EXIT_TIME_DESTRUCTORS
"Treat exit-time destructors in SDK libraries as errors (Clang only)."
${firebase_werror_exit_time_destructors_default})

option(FIREBASE_QUICK_TEST
"Enable quick tests will skip tests which requires access to the SECRET" OFF)

Expand Down Expand Up @@ -116,6 +127,7 @@ endif()
list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_CURRENT_LIST_DIR}/cmake)
include(external_rules)
include(cpp_pack)
include(exit_time_destructors)

if(FIREBASE_CPP_BUILD_TESTS OR FIREBASE_CPP_BUILD_STUB_TESTS)
enable_testing()
Expand Down
2 changes: 2 additions & 0 deletions analytics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ target_compile_definitions(firebase_analytics
-DINTERNAL_EXPERIMENTAL=1
)

firebase_cpp_reject_exit_time_destructors(firebase_analytics)



if(ANDROID)
Expand Down
42 changes: 30 additions & 12 deletions analytics/src/analytics_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "app/src/include/firebase/future.h"
#include "app/src/include/firebase/variant.h"
#include "app/src/log.h"
#include "app/src/no_destructor.h"
#include "firebase/log.h"

#if defined(_WIN32)
Expand All @@ -48,12 +49,29 @@ static HMODULE g_analytics_module = 0;
// Future data for analytics.
// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
static bool g_initialized = false;
static LogCallback g_log_callback;
static std::mutex g_log_callback_mutex;

static LogCallback& GetLogCallback() {
static NoDestructor<LogCallback> log_callback;
return *log_callback;
}

static std::mutex& LogCallbackMutex() {
static NoDestructor<std::mutex> log_callback_mutex;
return *log_callback_mutex;
}

static int g_fake_instance_id = 0;
static bool g_analytics_collection_enabled = true;
static std::string g_app_id;
static std::string g_package_name;

static std::string& AppId() {
static NoDestructor<std::string> app_id;
return *app_id;
}

static std::string& PackageName() {
static NoDestructor<std::string> package_name;
return *package_name;
}

// Initializes the Analytics desktop API.
// This function must be called before any other Analytics methods.
Expand Down Expand Up @@ -91,15 +109,15 @@ void Initialize(const App& app) {
LogInfo("Analytics: Loaded Google Analytics module.");

// Initialize Google Analytics C API
g_app_id = app.options().app_id();
g_package_name = app.options().package_name();
AppId() = app.options().app_id();
PackageName() = app.options().package_name();

GoogleAnalytics_Options* c_options = GoogleAnalytics_Options_Create();
if (!c_options) {
LogError("Analytics: Failed to create GoogleAnalytics_Options.");
} else {
c_options->app_id = g_app_id.c_str();
c_options->package_name = g_package_name.c_str();
c_options->app_id = AppId().c_str();
c_options->package_name = PackageName().c_str();
c_options->analytics_collection_enabled_at_first_launch =
g_analytics_collection_enabled;

Expand Down Expand Up @@ -416,8 +434,8 @@ void GoogleAnalyticsWrapperLogCallback(int32_t log_level, const char* message) {
LogCallback callback_to_call;

{
std::lock_guard<std::mutex> lock(g_log_callback_mutex);
callback_to_call = g_log_callback;
std::lock_guard<std::mutex> lock(LogCallbackMutex());
callback_to_call = GetLogCallback();
}

if (callback_to_call) {
Expand All @@ -433,8 +451,8 @@ void SetLogCallback(const LogCallback& callback) {
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized());
// The C API does not support user data, so we must use a global variable.
{
std::lock_guard<std::mutex> lock(g_log_callback_mutex);
g_log_callback = callback;
std::lock_guard<std::mutex> lock(LogCallbackMutex());
GetLogCallback() = callback;
}
if (callback) {
GoogleAnalytics_SetLogCallback(GoogleAnalyticsWrapperLogCallback);
Expand Down
30 changes: 17 additions & 13 deletions analytics/src/analytics_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "app/src/include/firebase/internal/mutex.h"
#include "app/src/include/firebase/version.h"
#include "app/src/log.h"
#include "app/src/no_destructor.h"
#include "app/src/thread.h"
#include "app/src/time.h"
#include "app/src/util.h"
Expand Down Expand Up @@ -120,13 +121,16 @@ uint64_t GetResetTimeElapsedMilliseconds() const {
const uint64_t AnalyticsDataResetter::kResetTimeoutMilliseconds = 5000;

static const double kMillisecondsPerSecond = 1000.0;
static Mutex g_mutex; // NOLINT
static Mutex& AnalyticsMutex() {
static NoDestructor<Mutex> mutex;
return *mutex;
}
static bool g_initialized = false;
static AnalyticsDataResetter* g_resetter = nullptr;

// Initialize the API.
void Initialize(const ::firebase::App& app) {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
g_initialized = true;
g_resetter = new AnalyticsDataResetter();
internal::RegisterTerminateOnDefaultAppDestroy();
Expand All @@ -142,7 +146,7 @@ void Initialize(const ::firebase::App& app) {

// Terminate the API.
void Terminate() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
internal::FutureData::Destroy();
internal::UnregisterTerminateOnDefaultAppDestroy();
delete g_resetter;
Expand Down Expand Up @@ -237,7 +241,7 @@ void LogEvent(const char* name) {
}

Future<void> LogAppleTransaction(const char* transaction_id) {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN(Future<void>(), internal::IsInitialized());

auto* api = internal::FutureData::Get()->api();
Expand All @@ -251,7 +255,7 @@ void LogEvent(const char* name) {
[AppleTransactionVerifier
verifyWithTransactionId:SafeString(transaction_id)
completion:^(BOOL isFound) {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
if (!internal::IsInitialized()) return;

auto* api = internal::FutureData::Get()->api();
Expand All @@ -266,7 +270,7 @@ void LogEvent(const char* name) {
}

Future<void> LogAppleTransactionLastResult() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN(Future<void>(), internal::IsInitialized());
return static_cast<const Future<void>&>(
internal::FutureData::Get()->api()->LastResult(internal::kAnalyticsFnLogAppleTransaction));
Expand Down Expand Up @@ -449,7 +453,7 @@ void SetSessionTimeoutDuration(int64_t milliseconds) {
}

void ResetAnalyticsData() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized());
g_resetter->Reset();
}
Expand All @@ -467,7 +471,7 @@ void NotifyAppLifecycleChange(AppLifecycleState) {}
void SetDesktopDebugMode(bool) {}

Future<std::string> GetAnalyticsInstanceId() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN(Future<std::string>(), internal::IsInitialized());
auto* api = internal::FutureData::Get()->api();
const auto future_handle =
Expand All @@ -477,7 +481,7 @@ Thread get_id_thread(
[](SafeFutureHandle<std::string>* handle) {
for (;;) {
{
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
if (!internal::IsInitialized()) break;
std::string instance_id = g_resetter->GetInstanceId();
if (!instance_id.empty()) {
Expand All @@ -495,19 +499,19 @@ Thread get_id_thread(
}

Future<std::string> GetAnalyticsInstanceIdLastResult() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN(Future<std::string>(), internal::IsInitialized());
return static_cast<const Future<std::string>&>(
internal::FutureData::Get()->api()->LastResult(internal::kAnalyticsFnGetAnalyticsInstanceId));
}

Future<int64_t> GetSessionId() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN(Future<int64_t>(), internal::IsInitialized());
auto* api = internal::FutureData::Get()->api();
const auto future_handle = api->SafeAlloc<int64_t>(internal::kAnalyticsFnGetSessionId);
[FIRAnalytics sessionIDWithCompletion:^(int64_t session_id, NSError* _Nullable error) {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
if (!internal::IsInitialized()) return;
if (error) {
api->Complete(future_handle, -1, util::NSStringToString(error.localizedDescription).c_str());
Expand All @@ -519,7 +523,7 @@ Thread get_id_thread(
}

Future<int64_t> GetSessionIdLastResult() {
MutexLock lock(g_mutex);
MutexLock lock(AnalyticsMutex());
FIREBASE_ASSERT_RETURN(Future<int64_t>(), internal::IsInitialized());
return static_cast<const Future<int64_t>&>(
internal::FutureData::Get()->api()->LastResult(internal::kAnalyticsFnGetSessionId));
Expand Down
9 changes: 9 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ set(utility_common_HDRS
src/future_manager.h
src/intrusive_list.h
src/log.h
src/no_destructor.h
src/optional.h
src/path.h
src/pthread_condvar.h
Expand Down Expand Up @@ -301,6 +302,12 @@ if(ANDROID OR IOS)
${FLATBUFFERS_SOURCE_DIR}/src/idl_gen_text.cpp
${FLATBUFFERS_SOURCE_DIR}/src/reflection.cpp
${FLATBUFFERS_SOURCE_DIR}/src/util.cpp)
# Third-party FlatBuffers sources define static globals with non-trivial
# destructors; exempt them from the SDK's exit-time destructor check below.
set_source_files_properties(
${app_flatbuffers_srcs}
PROPERTIES COMPILE_OPTIONS
"$<$<CXX_COMPILER_ID:Clang,AppleClang>:-Wno-exit-time-destructors>")
set(app_flatbuffers_lib)
else()
set(app_flatbuffers_srcs)
Expand Down Expand Up @@ -331,6 +338,8 @@ else()
target_compile_options(firebase_app PRIVATE -fno-exceptions)
endif()

firebase_cpp_reject_exit_time_destructors(firebase_app)

target_include_directories(firebase_app
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src/include
Expand Down
2 changes: 2 additions & 0 deletions app/rest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ set(rest_SRCS
add_library(firebase_rest_lib STATIC
${rest_SRCS})

firebase_cpp_reject_exit_time_destructors(firebase_rest_lib)

target_include_directories(firebase_rest_lib
PUBLIC
${FIREBASE_CPP_SDK_ROOT_DIR}
Expand Down
18 changes: 12 additions & 6 deletions app/src/app_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "app/src/include/firebase/internal/mutex.h"
#include "app/src/include/firebase/internal/platform.h"
#include "app/src/include/firebase/version.h"
#include "app/src/no_destructor.h"
#include "app/src/util.h"

// strtok_r is strtok_s on Windows.
Expand Down Expand Up @@ -170,13 +171,18 @@ const char* kBuildSource = "custom_built";
const char* kApiClientHeader = "x-firebase-client";
const char* kXFirebaseGmpIdHeader = "X-Firebase-GMPID";

SystemLogger g_system_logger; // NOLINT
// Wrapped in NoDestructor so per-App loggers remain valid if static Firebase
// instances log during process exit.
static SystemLogger& GetSystemLogger() {
static NoDestructor<SystemLogger> system_logger;
return *system_logger;
}

// Private cross platform data associated with an app.
struct AppData {
// TODO(b/140528778): Remove kLogLevelVerbose here and add [GS]etLogLevel
// member functions to app.
AppData() : logger(&g_system_logger, kLogLevelVerbose) {}
AppData() : logger(&GetSystemLogger(), kLogLevelVerbose) {}

// App associated with this data.
App* app;
Expand Down Expand Up @@ -376,10 +382,10 @@ void RemoveApp(App* app) {
delete g_apps;
g_apps = nullptr;
}
}
callback::Terminate(last_app);
if (last_app) {
LibraryRegistry::Terminate();
callback::Terminate(last_app);
if (last_app) {
LibraryRegistry::Terminate();
}
}
}
}
Expand Down
Loading
Loading