Skip to content

Commit 3944b11

Browse files
authored
Fix Mutex destructors being incorrectly run at exit (cl/364325997) (#345)
1 parent 1ea86a7 commit 3944b11

File tree

14 files changed

+63
-60
lines changed

14 files changed

+63
-60
lines changed

app/rest/transport_curl.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ CurlThread* g_curl_thread = nullptr;
312312
int g_initialize_count = 0;
313313

314314
// Mutex for Curl initialization.
315-
Mutex g_initialize_mutex; // NOLINT
315+
Mutex* g_initialize_mutex = new Mutex();
316316

317317
} // namespace
318318

319319
void InitTransportCurl() {
320-
MutexLock lock(g_initialize_mutex);
320+
MutexLock lock(*g_initialize_mutex);
321321
if (g_initialize_count == 0) {
322322
// Initialize curl.
323323
CURLcode global_init_code = curl_global_init(CURL_GLOBAL_ALL);
@@ -333,7 +333,7 @@ void InitTransportCurl() {
333333
}
334334

335335
void CleanupTransportCurl() {
336-
MutexLock lock(g_initialize_mutex);
336+
MutexLock lock(*g_initialize_mutex);
337337
assert(g_initialize_count > 0);
338338
g_initialize_count--;
339339
if (g_initialize_count == 0) {

app/rest/util.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ const char kGet[] = "GET";
4545
const char kPost[] = "POST";
4646

4747
// Mutex for utils initialization and distribution of CURL pointers.
48-
static ::firebase::Mutex g_util_curl_mutex; // NOLINT
48+
static ::firebase::Mutex* g_util_curl_mutex = new Mutex();
4949

5050
CURL* g_curl_instance = nullptr;
5151
int g_init_ref_count = 0;
5252

5353
void* CreateCurlPtr() { // NOLINT
54-
MutexLock lock(g_util_curl_mutex);
54+
MutexLock lock(*g_util_curl_mutex);
5555
return curl_easy_init();
5656
}
5757

5858
void DestroyCurlPtr(void* curl_ptr) {
59-
MutexLock lock(g_util_curl_mutex);
59+
MutexLock lock(*g_util_curl_mutex);
6060
curl_easy_cleanup(static_cast<CURL*>(curl_ptr));
6161
}
6262

6363
void Initialize() {
64-
MutexLock curl_lock(g_util_curl_mutex);
64+
MutexLock curl_lock(*g_util_curl_mutex);
6565
assert(g_init_ref_count >= 0);
6666
if (g_init_ref_count == 0) {
6767
g_curl_instance = reinterpret_cast<CURL*>(CreateCurlPtr());
@@ -70,7 +70,7 @@ void Initialize() {
7070
}
7171

7272
void Terminate() {
73-
MutexLock curl_lock(g_util_curl_mutex);
73+
MutexLock curl_lock(*g_util_curl_mutex);
7474
--g_init_ref_count;
7575
assert(g_init_ref_count >= 0);
7676
if (g_init_ref_count == 0) {

app/src/app_common.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class LibraryRegistry {
255255
};
256256

257257
// Guards g_apps and g_default_app.
258-
static Mutex g_app_mutex; // NOLINT
258+
static Mutex* g_app_mutex = new Mutex();
259259
static std::map<std::string, UniquePtr<AppData>>* g_apps;
260260
static App* g_default_app = nullptr;
261261
LibraryRegistry* LibraryRegistry::library_registry_ = nullptr;
@@ -265,7 +265,7 @@ App* AddApp(App* app, std::map<std::string, InitResult>* results) {
265265
assert(app);
266266
App* existing_app = FindAppByName(app->name());
267267
FIREBASE_ASSERT_RETURN(nullptr, !existing_app);
268-
MutexLock lock(g_app_mutex);
268+
MutexLock lock(*g_app_mutex);
269269
if (IsDefaultAppName(app->name())) {
270270
assert(!g_default_app);
271271
g_default_app = app;
@@ -309,7 +309,7 @@ App* AddApp(App* app, std::map<std::string, InitResult>* results) {
309309

310310
App* FindAppByName(const char* name) {
311311
assert(name);
312-
MutexLock lock(g_app_mutex);
312+
MutexLock lock(*g_app_mutex);
313313
if (g_apps) {
314314
auto it = g_apps->find(std::string(name));
315315
if (it == g_apps->end()) return nullptr;
@@ -325,7 +325,7 @@ App* GetAnyApp() {
325325
return g_default_app;
326326
}
327327

328-
MutexLock lock(g_app_mutex);
328+
MutexLock lock(*g_app_mutex);
329329
if (g_apps && !g_apps->empty()) {
330330
return g_apps->begin()->second->app;
331331
}
@@ -334,7 +334,7 @@ App* GetAnyApp() {
334334

335335
void RemoveApp(App* app) {
336336
assert(app);
337-
MutexLock lock(g_app_mutex);
337+
MutexLock lock(*g_app_mutex);
338338
if (g_apps) {
339339
auto it = g_apps->find(std::string(app->name()));
340340
bool last_app = false;
@@ -369,7 +369,7 @@ void RemoveApp(App* app) {
369369
void DestroyAllApps() {
370370
std::vector<App*> apps_to_delete;
371371
App* const default_app = GetDefaultApp();
372-
MutexLock lock(g_app_mutex);
372+
MutexLock lock(*g_app_mutex);
373373
if (g_apps) {
374374
for (auto it = g_apps->begin(); it != g_apps->end(); ++it) {
375375
if (it->second->app != default_app)
@@ -391,15 +391,15 @@ bool IsDefaultAppName(const char* name) {
391391
}
392392

393393
void RegisterLibrary(const char* library, const char* version) {
394-
MutexLock lock(g_app_mutex);
394+
MutexLock lock(*g_app_mutex);
395395
LibraryRegistry* registry = LibraryRegistry::Initialize();
396396
if (registry->RegisterLibrary(library, version)) {
397397
registry->UpdateUserAgent();
398398
}
399399
}
400400

401401
void RegisterLibrariesFromUserAgent(const char* user_agent) {
402-
MutexLock lock(g_app_mutex);
402+
MutexLock lock(*g_app_mutex);
403403
LibraryRegistry* registry = LibraryRegistry::Initialize();
404404
// Copy the string into a vector so that we can safely mutate the string.
405405
std::vector<char> user_agent_vector(user_agent,
@@ -427,12 +427,12 @@ void RegisterLibrariesFromUserAgent(const char* user_agent) {
427427
}
428428

429429
const char* GetUserAgent() {
430-
MutexLock lock(g_app_mutex);
430+
MutexLock lock(*g_app_mutex);
431431
return LibraryRegistry::Initialize()->GetUserAgent();
432432
}
433433

434434
std::string GetLibraryVersion(const char* library) {
435-
MutexLock lock(g_app_mutex);
435+
MutexLock lock(*g_app_mutex);
436436
return LibraryRegistry::Initialize()->GetLibraryVersion(library);
437437
}
438438

@@ -442,7 +442,7 @@ void GetOuterMostSdkAndVersion(std::string* sdk, std::string* version) {
442442
sdk->clear();
443443
version->clear();
444444

445-
MutexLock lock(g_app_mutex);
445+
MutexLock lock(*g_app_mutex);
446446
// Set of library versions to query in order of outer wrapper to inner.
447447
// We're only retrieving the outer-most SDK version here as we can only send
448448
// one component as part of the user agent string to the storage backend.
@@ -466,7 +466,7 @@ void GetOuterMostSdkAndVersion(std::string* sdk, std::string* version) {
466466
// Find a logger associated with an app by app name.
467467
Logger* FindAppLoggerByName(const char* name) {
468468
assert(name);
469-
MutexLock lock(g_app_mutex);
469+
MutexLock lock(*g_app_mutex);
470470
if (g_apps) {
471471
auto it = g_apps->find(std::string(name));
472472
if (it == g_apps->end()) return nullptr;

app/src/callback.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ class CallbackDispatcher {
187187

188188
static CallbackDispatcher* g_callback_dispatcher = nullptr;
189189
// Mutex that controls access to g_callback_dispatcher and g_callback_ref_count.
190-
static Mutex g_callback_mutex; // NOLINT
190+
static Mutex* g_callback_mutex = new Mutex();
191191
static int g_callback_ref_count = 0;
192192
static Thread::Id g_callback_thread_id;
193193
static bool g_callback_thread_id_initialized = false;
194194

195195
void Initialize() {
196-
MutexLock lock(g_callback_mutex);
196+
MutexLock lock(*g_callback_mutex);
197197
if (g_callback_ref_count == 0) {
198198
g_callback_dispatcher = new CallbackDispatcher();
199199
}
@@ -202,7 +202,7 @@ void Initialize() {
202202

203203
// Add a reference to the module if it's already initialized.
204204
static bool InitializeIfInitialized() {
205-
MutexLock lock(g_callback_mutex);
205+
MutexLock lock(*g_callback_mutex);
206206
if (IsInitialized()) {
207207
Initialize();
208208
return true;
@@ -217,7 +217,7 @@ bool IsInitialized() { return g_callback_ref_count > 0; }
217217
static void Terminate(int number_of_references_to_remove) {
218218
CallbackDispatcher* dispatcher_to_destroy = nullptr;
219219
{
220-
MutexLock lock(g_callback_mutex);
220+
MutexLock lock(*g_callback_mutex);
221221
if (!g_callback_ref_count) {
222222
LogWarning("Callback module already shut down");
223223
return;
@@ -239,7 +239,7 @@ static void Terminate(int number_of_references_to_remove) {
239239
}
240240

241241
void Terminate(bool flush_all) {
242-
MutexLock lock(g_callback_mutex);
242+
MutexLock lock(*g_callback_mutex);
243243
int ref_count = 1;
244244
// g_callback_ref_count is used to track the number of current references to
245245
// g_callback_dispatcher so we need to decrement the reference count by just
@@ -253,7 +253,7 @@ void Terminate(bool flush_all) {
253253
}
254254

255255
void* AddCallback(Callback* callback) {
256-
MutexLock lock(g_callback_mutex);
256+
MutexLock lock(*g_callback_mutex);
257257
Initialize();
258258
return g_callback_dispatcher->AddCallback(callback);
259259
}

app/src/cleanup_notifier.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
namespace FIREBASE_NAMESPACE {
2828

29-
Mutex CleanupNotifier::cleanup_notifiers_by_owner_mutex_; // NOLINT
29+
Mutex* CleanupNotifier::cleanup_notifiers_by_owner_mutex_ = new Mutex();
3030
std::map<void *, CleanupNotifier *>
3131
*CleanupNotifier::cleanup_notifiers_by_owner_;
3232

3333
CleanupNotifier::CleanupNotifier() : cleaned_up_(false) {
34-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
34+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
3535
if (!cleanup_notifiers_by_owner_) {
3636
cleanup_notifiers_by_owner_ = new std::map<void *, CleanupNotifier *>();
3737
}
@@ -41,7 +41,7 @@ CleanupNotifier::~CleanupNotifier() {
4141
CleanupAll();
4242
UnregisterAllOwners();
4343
{
44-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
44+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
4545
if (cleanup_notifiers_by_owner_ && cleanup_notifiers_by_owner_->empty()) {
4646
delete cleanup_notifiers_by_owner_;
4747
cleanup_notifiers_by_owner_ = nullptr;
@@ -78,14 +78,14 @@ void CleanupNotifier::CleanupAll() {
7878
}
7979

8080
void CleanupNotifier::UnregisterAllOwners() {
81-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
81+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
8282
while (owners_.begin() != owners_.end()) {
8383
UnregisterOwner(this, owners_[0]);
8484
}
8585
}
8686

8787
void CleanupNotifier::RegisterOwner(CleanupNotifier *notifier, void *owner) {
88-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
88+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
8989
assert(cleanup_notifiers_by_owner_);
9090
auto it = cleanup_notifiers_by_owner_->find(owner);
9191
if (it != cleanup_notifiers_by_owner_->end()) UnregisterOwner(it);
@@ -94,15 +94,15 @@ void CleanupNotifier::RegisterOwner(CleanupNotifier *notifier, void *owner) {
9494
}
9595

9696
void CleanupNotifier::UnregisterOwner(CleanupNotifier *notifier, void *owner) {
97-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
97+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
9898
assert(cleanup_notifiers_by_owner_);
9999
auto it = cleanup_notifiers_by_owner_->find(owner);
100100
if (it != cleanup_notifiers_by_owner_->end()) UnregisterOwner(it);
101101
}
102102

103103
void CleanupNotifier::UnregisterOwner(
104104
std::map<void *, CleanupNotifier *>::iterator it) {
105-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
105+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
106106
assert(cleanup_notifiers_by_owner_);
107107
void *owner = it->first;
108108
CleanupNotifier *notifier = it->second;
@@ -114,7 +114,7 @@ void CleanupNotifier::UnregisterOwner(
114114
}
115115

116116
CleanupNotifier *CleanupNotifier::FindByOwner(void *owner) {
117-
MutexLock lock(cleanup_notifiers_by_owner_mutex_);
117+
MutexLock lock(*cleanup_notifiers_by_owner_mutex_);
118118
if (!cleanup_notifiers_by_owner_) return nullptr;
119119
auto it = cleanup_notifiers_by_owner_->find(owner);
120120
return it != cleanup_notifiers_by_owner_->end() ? it->second : nullptr;

app/src/cleanup_notifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class CleanupNotifier {
102102
std::vector<void *> owners_;
103103

104104
// Guards owners_ and cleanup_notifiers_by_owner_.
105-
static Mutex cleanup_notifiers_by_owner_mutex_;
105+
static Mutex* cleanup_notifiers_by_owner_mutex_;
106106
// Global map of cleanup notifiers bucketed by owner object.
107107
static std::map<void *, CleanupNotifier *> *cleanup_notifiers_by_owner_;
108108
};

app/src/secure/user_secure_manager.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace secure {
5050

5151
using callback::NewCallback;
5252

53-
Mutex UserSecureManager::s_scheduler_mutex_; // NOLINT
53+
Mutex* UserSecureManager::s_scheduler_mutex_ = new Mutex();
5454
scheduler::Scheduler* UserSecureManager::s_scheduler_;
5555
int32_t UserSecureManager::s_scheduler_ref_count_;
5656

@@ -184,7 +184,7 @@ Future<void> UserSecureManager::DeleteAllData() {
184184
}
185185

186186
void UserSecureManager::CreateScheduler() {
187-
MutexLock lock(s_scheduler_mutex_);
187+
MutexLock lock(*s_scheduler_mutex_);
188188
if (s_scheduler_ == nullptr) {
189189
s_scheduler_ = new scheduler::Scheduler();
190190
// reset count
@@ -194,7 +194,7 @@ void UserSecureManager::CreateScheduler() {
194194
}
195195

196196
void UserSecureManager::DestroyScheduler() {
197-
MutexLock lock(s_scheduler_mutex_);
197+
MutexLock lock(*s_scheduler_mutex_);
198198
if (s_scheduler_ == nullptr) {
199199
// reset count
200200
s_scheduler_ref_count_ = 0;

app/src/secure/user_secure_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class UserSecureManager {
7474
static void DestroyScheduler();
7575

7676
// Guards static scheduler pointer
77-
static Mutex s_scheduler_mutex_; // NOLINT
77+
static Mutex* s_scheduler_mutex_;
7878
static scheduler::Scheduler* s_scheduler_;
7979
static int32_t s_scheduler_ref_count_;
8080

0 commit comments

Comments
 (0)