From fd928299133f7539a2f0d8ec2985de95f3cb2225 Mon Sep 17 00:00:00 2001 From: Ken Zangelin Date: Thu, 8 Feb 2024 12:55:32 +0100 Subject: [PATCH] Desperate attempt to fix issue #1499 --- src/lib/orionld/mongoc/mongocGeoIndexInit.cpp | 2 ++ .../mongoc/mongocRegistrationsIter.cpp | 3 ++ .../mongoc/mongocSubCachePopulateByTenant.cpp | 3 ++ .../mongoc/mongocSubCountersUpdate.cpp | 3 ++ src/lib/rest/rest.cpp | 28 +++++++++++++++---- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp b/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp index c6151b7837..5842ba3f7a 100644 --- a/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp +++ b/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp @@ -72,6 +72,8 @@ bool mongocGeoIndexInit(void) // Get handle to collection // mCollectionP = mongoc_client_get_collection(orionldState.mongoc.client, tenantP->mongoDbName, "entities"); + if (mCollectionP == NULL) + LM_X(1, ("mongoc_client_get_collection failed for 'entities' collection on tenant '%s'", tenantP->mongoDbName)); // Aggregation pipeline diff --git a/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp b/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp index c8f7f8a8cd..42594f6ee8 100644 --- a/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp +++ b/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp @@ -69,6 +69,9 @@ int mongocRegistrationsIter(RegCache* rcP, RegCacheIterFunc callback) // mongoc_collection_t* regsCollectionP = mongoc_client_get_collection(orionldState.mongoc.client, rcP->tenantP->mongoDbName, "registrations"); + if (regsCollectionP == NULL) + LM_X(1, ("mongoc_client_get_collection failed for 'registrations' collection on tenant '%s'", rcP->tenantP->mongoDbName)); + // // Run the query // diff --git a/src/lib/orionld/mongoc/mongocSubCachePopulateByTenant.cpp b/src/lib/orionld/mongoc/mongocSubCachePopulateByTenant.cpp index 12f16f5fe7..18f3f3ce5e 100644 --- a/src/lib/orionld/mongoc/mongocSubCachePopulateByTenant.cpp +++ b/src/lib/orionld/mongoc/mongocSubCachePopulateByTenant.cpp @@ -78,6 +78,9 @@ bool mongocSubCachePopulateByTenant(OrionldTenant* tenantP, bool refresh) mongoc_client_t* connectionP = mongoc_client_pool_pop(mongocPool); mongoc_collection_t* subscriptionsP = mongoc_client_get_collection(connectionP, tenantP->mongoDbName, "csubs"); + if (subscriptionsP == NULL) + LM_X(1, ("mongoc_client_get_collection failed for 'csubs' collection on tenant '%s'", tenantP->mongoDbName)); + // // Run the query // diff --git a/src/lib/orionld/mongoc/mongocSubCountersUpdate.cpp b/src/lib/orionld/mongoc/mongocSubCountersUpdate.cpp index a723d75cbe..3154d66640 100644 --- a/src/lib/orionld/mongoc/mongocSubCountersUpdate.cpp +++ b/src/lib/orionld/mongoc/mongocSubCountersUpdate.cpp @@ -83,6 +83,9 @@ void mongocSubCountersUpdate bson_t selector; bson_t inc; + if (subscriptionsP == NULL) + LM_X(1, ("mongoc_client_get_collection failed for 'csubs' collection on tenant '%s'", tenantP->mongoDbName)); + bson_init(&reply); bson_init(&selector); bson_init(&inc); diff --git a/src/lib/rest/rest.cpp b/src/lib/rest/rest.cpp index f8a3428345..2f4d7457e0 100644 --- a/src/lib/rest/rest.cpp +++ b/src/lib/rest/rest.cpp @@ -391,12 +391,28 @@ static void requestCompleted // - // Release the connection to mongo - after all notifications have been sent (orionldAlterationsTreat takes care of that) - // - if (orionldState.mongoc.contextsP) mongoc_collection_destroy(orionldState.mongoc.contextsP); - if (orionldState.mongoc.entitiesP) mongoc_collection_destroy(orionldState.mongoc.entitiesP); - if (orionldState.mongoc.subscriptionsP) mongoc_collection_destroy(orionldState.mongoc.subscriptionsP); - if (orionldState.mongoc.registrationsP) mongoc_collection_destroy(orionldState.mongoc.registrationsP); + // Release the connections to mongo - after all notifications have been sent (orionldAlterationsTreat takes care of the notifications) + // NOTE, this "construct" with NULLing the mongoc_collection_t pointers before actually calling mongoc_collection_destroy is a + // desperate attempt to fix the issue #1499. + // If still not enough, perhaps a semaphore to protect. + // It's weird though, these collection pointers are THREAD VARIABLES !!! + // No protection should be needed. + // But, desperate times ... + // + mongoc_collection_t* contextsP = orionldState.mongoc.contextsP; + mongoc_collection_t* entitiesP = orionldState.mongoc.entitiesP; + mongoc_collection_t* subscriptionsP = orionldState.mongoc.subscriptionsP; + mongoc_collection_t* registrationsP = orionldState.mongoc.registrationsP; + + orionldState.mongoc.contextsP = NULL; + orionldState.mongoc.entitiesP = NULL; + orionldState.mongoc.subscriptionsP = NULL; + orionldState.mongoc.registrationsP = NULL; + + if (contextsP != NULL) mongoc_collection_destroy(contextsP); + if (entitiesP != NULL) mongoc_collection_destroy(entitiesP); + if (subscriptionsP != NULL) mongoc_collection_destroy(subscriptionsP); + if (registrationsP != NULL) mongoc_collection_destroy(registrationsP); mongocConnectionRelease();