From cf0a6386d33e1311a6f37a887872949d95f1cc16 Mon Sep 17 00:00:00 2001 From: Kevin Guillemot Date: Wed, 30 Jun 2021 17:39:13 +0200 Subject: [PATCH] OMMONGODB :: Fixes - Fix Segmentation fault when server is down - Add server connexion check while resuming trust merge open source commit:cf0a6386d33e1311a6f37a887872949d95f1cc16 --- plugins/ommongodb/ommongodb.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/ommongodb/ommongodb.c b/plugins/ommongodb/ommongodb.c index fc3153afd..a8b6ac96f 100644 --- a/plugins/ommongodb/ommongodb.c +++ b/plugins/ommongodb/ommongodb.c @@ -136,10 +136,12 @@ static void closeMongoDB(instanceData *pData) if(pData->client != NULL) { if (pData->collection != NULL) { mongoc_collection_destroy (pData->collection); + pData->collection = NULL; } - mongoc_client_destroy (pData->client); + pData->client = NULL; mongoc_cleanup (); + DBGPRINTF("ommongodb: Mongodb connexion closed."); } } @@ -189,6 +191,7 @@ reportMongoError(instanceData *pData) /* The following function is responsible for initializing a * MongoDB connection. * Initially added 2004-10-28 mmeckelein + * Improved to check if server is available (ping) @kguillemot 2021-06-30 */ static rsRetVal initMongoDB(instanceData *pData, int bSilent) { @@ -208,7 +211,7 @@ static rsRetVal initMongoDB(instanceData *pData, int bSilent) dbgprintf("ommongodb: mongo-c-driver was not built with SSL options, ssl directives will not be used."); #endif } - if(pData->client == NULL) { + if(!pData->client) { if(!bSilent) { reportMongoError(pData); dbgprintf("ommongodb: can not initialize MongoDB handle"); @@ -217,6 +220,20 @@ static rsRetVal initMongoDB(instanceData *pData, int bSilent) } pData->collection = mongoc_client_get_collection (pData->client, pData->db, pData->collection_name); + // Try to contact server + bson_t *command, reply; + bson_error_t error; + command = BCON_NEW ("ping", BCON_INT32 (1)); + unsigned char retval = mongoc_client_command_simple(pData->client, pData->db, command, NULL, &reply, &error); + bson_destroy(&reply); + bson_destroy(command); + if( !retval ) { + DBGPRINTF("ommongodb: ping server error (%u): %s \n", error.code, error.message); + closeMongoDB(pData); + reportMongoError(pData); + ABORT_FINALIZE(RS_RET_SUSPENDED); + } + finalize_it: RETiRet; } @@ -551,7 +568,7 @@ CODESTARTdoAction } else if (is_allowed_error_code(pData, pData->error.code)) { dbgprintf("ommongodb: insert error: allowing error code\n"); } else { - dbgprintf("ommongodb: insert error\n"); + dbgprintf("ommongodb: insert error %u : %s \n", pData->error.code, pData->error.message); reportMongoError(pData); /* close on insert error to permit resume */ closeMongoDB(pData); -- 2.23.0