RDKCOM-5467: RDKDEV-1147 Improvement in RDK Logger functionalities to handle race conditions#35
Conversation
sushil-shinde-infosys
commented
Nov 10, 2025
|
All contributors have signed the CLA ✍️ ✅ |
03316e6 to
95ddf65
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses critical race conditions in the RDK Logger that were causing crashes due to concurrent access to log4c's non-thread-safe hash table. The root cause was identified as strcmp() receiving invalid pointers from sd_hash_lookup() when multiple threads accessed the logger concurrently. The solution introduces synchronization mechanisms to protect initialization, logging operations, and shutdown sequences.
Key Changes:
- Added mutex protection around logger initialization to prevent concurrent initialization attempts
- Introduced atomic counter tracking active log calls with a wait mechanism in deinit to ensure graceful shutdown
- Protected log4c category access with a dedicated mutex to prevent concurrent access to log4c's internal data structures
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 12 comments.
| File | Description |
|---|---|
| src/rdk_logger_init.c | Added initialization mutex, atomic counter for tracking active log calls, and shutdown wait mechanism with timeout to ensure all logging completes before cleanup |
| src/rdk_debug_priv.c | Added mutex protection around log4c_category_get() calls, atomic counter for tracking active vlog calls, and refactored switch statement for cleaner code |
Comments suppressed due to low confidence (1)
src/rdk_logger_init.c:100
- Missing mutex unlock when
isLogInitedis already 1. Ifrdk_logger_init()is called when the logger is already initialized (line 69 check fails), the function returns at line 100 without unlocking the mutex that was locked at line 68, causing a deadlock on subsequent calls.
Add pthread_mutex_unlock(&g_rdk_logger_init_mutex); before line 100, or restructure the code to ensure the mutex is always unlocked on all paths.
if (0 == isLogInited)
{
if (NULL == debugConfigFile)
{
debugConfigFile = DEBUG_CONF_FILE;
}
/* Read the config file & populate pre-configured log levels */
ret = rdk_logger_parse_config(debugConfigFile);
if ( RDK_SUCCESS != ret)
{
printf("%s:%d Adding debug config file %s failed\n", __FUNCTION__, __LINE__, debugConfigFile);
pthread_mutex_unlock(&g_rdk_logger_init_mutex);
return ret;
}
/* Perform Logger Internal Init */
rdk_dbg_init();
/* Perform Dynamin Logger Internal Init */
rdk_dyn_log_init();
pthread_mutex_unlock(&g_rdk_logger_init_mutex);
/**
* Requests not to send SIGPIPE on errors on stream oriented
* sockets when the other end breaks the connection. The EPIPE
* error is still returned.
*/
signal(SIGPIPE, SIG_IGN);
isLogInited = 1;
}
return RDK_SUCCESS;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/rdk_debug_priv.c
Outdated
|
|
||
| pthread_mutex_unlock(&g_log4c_cat_get_mutex); | ||
|
|
||
| if (!IS_LOGGING_ENABLED_FOR_LEVEL(module_name, level)) |
There was a problem hiding this comment.
The IS_LOGGING_ENABLED_FOR_LEVEL macro calls log4c_category_get(module_name) again at line 397 without mutex protection. This is a TOCTOU (time-of-check-time-of-use) issue. The category obtained under mutex protection (line 376) could be modified or invalidated between the mutex unlock (line 395) and this check.
Store the result of the enabled check while still holding the mutex, or extend the mutex protection to cover this check as well.
| pthread_mutex_unlock(&g_log4c_cat_get_mutex); | |
| if (!IS_LOGGING_ENABLED_FOR_LEVEL(module_name, level)) | |
| int is_enabled = 0; | |
| /* Check if logging is enabled for this level while still holding the mutex */ | |
| if (cat) { | |
| int cat_priority = log4c_category_get_priority(cat); | |
| int req_priority = rdk_logLevel_to_log4c_priority(level); | |
| is_enabled = (cat_priority != LOG4C_PRIORITY_NONE && req_priority <= cat_priority); | |
| } | |
| pthread_mutex_unlock(&g_log4c_cat_get_mutex); | |
| if (!is_enabled) |
There was a problem hiding this comment.
Its valid suggestion.
src/rdk_logger_init.c
Outdated
| @@ -78,6 +87,8 @@ rdk_Error rdk_logger_init(const char* debugConfigFile) | |||
| /* Perform Dynamin Logger Internal Init */ | |||
There was a problem hiding this comment.
Typo in comment: "Dynamin" should be "Dynamic".
| /* Perform Dynamin Logger Internal Init */ | |
| /* Perform Dynamic Logger Internal Init */ |
sushil-shinde-infosys
left a comment
There was a problem hiding this comment.
valid recommendations are addressed, few are ignored.
|
recheck |
|
I have read the CLA Document and I hereby sign the CLA |
b8ee73b to
ea90f87
Compare