From 1f6b7bbcff352abe0ba0a865be27f63e975f0482 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 27 Apr 2024 10:08:15 +0800 Subject: [PATCH] fix(util): fix dead lock. --- include/util/tconfig.h | 2 +- source/client/src/clientEnv.c | 2 +- source/common/src/tglobal.c | 21 +++++++++++++-------- source/libs/command/src/command.c | 2 +- source/util/src/tconfig.c | 26 ++++++++++++++++---------- 5 files changed, 32 insertions(+), 21 deletions(-) diff --git a/include/util/tconfig.h b/include/util/tconfig.h index 4c16ed0ea6..8373e00085 100644 --- a/include/util/tconfig.h +++ b/include/util/tconfig.h @@ -107,7 +107,7 @@ int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs); // SConfigPair void cfgCleanup(SConfig *pCfg); int32_t cfgGetSize(SConfig *pCfg); SConfigItem *cfgGetItem(SConfig *pCfg, const char *pName); -int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype); +int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype, bool lock); int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer); SConfigIter *cfgCreateIter(SConfig *pConf); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index f37e9851e1..439103e5c4 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -838,7 +838,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { return -1; } - int code = cfgSetItem(pCfg, name, str, CFG_STYPE_TAOS_OPTIONS); + int code = cfgSetItem(pCfg, name, str, CFG_STYPE_TAOS_OPTIONS, true); if (code != 0) { tscError("failed to set cfg:%s to %s since %s", name, str, terrstr()); } else { diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index a839da7264..cad1145a6b 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -1081,13 +1081,13 @@ static int32_t taosSetClientCfg(SConfig *pCfg) { SEp firstEp = {0}; taosGetFqdnPortFromEp(strlen(pFirstEpItem->str) == 0 ? defaultFirstEp : pFirstEpItem->str, &firstEp); snprintf(tsFirst, sizeof(tsFirst), "%s:%u", firstEp.fqdn, firstEp.port); - cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype); + cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype, true); SConfigItem *pSecondpItem = cfgGetItem(pCfg, "secondEp"); SEp secondEp = {0}; taosGetFqdnPortFromEp(strlen(pSecondpItem->str) == 0 ? defaultFirstEp : pSecondpItem->str, &secondEp); snprintf(tsSecond, sizeof(tsSecond), "%s:%u", secondEp.fqdn, secondEp.port); - cfgSetItem(pCfg, "secondEp", tsSecond, pSecondpItem->stype); + cfgSetItem(pCfg, "secondEp", tsSecond, pSecondpItem->stype, true); tstrncpy(tsTempDir, cfgGetItem(pCfg, "tempDir")->str, PATH_MAX); taosExpandDir(tsTempDir, tsTempDir, PATH_MAX); @@ -1149,9 +1149,10 @@ static int32_t taosSetClientCfg(SConfig *pCfg) { static void taosSetSystemCfg(SConfig *pCfg) { SConfigItem *pItem = cfgGetItem(pCfg, "timezone"); + osSetTimezone(pItem->str); uDebug("timezone format changed from %s to %s", pItem->str, tsTimezoneStr); - cfgSetItem(pCfg, "timezone", tsTimezoneStr, pItem->stype); + cfgSetItem(pCfg, "timezone", tsTimezoneStr, pItem->stype, true); const char *locale = cfgGetItem(pCfg, "locale")->str; const char *charset = cfgGetItem(pCfg, "charset")->str; @@ -1639,7 +1640,8 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { SEp firstEp = {0}; taosGetFqdnPortFromEp(strlen(pFirstEpItem->str) == 0 ? defaultFirstEp : pFirstEpItem->str, &firstEp); snprintf(tsFirst, sizeof(tsFirst), "%s:%u", firstEp.fqdn, firstEp.port); - cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype); + + cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype, false); uInfo("localEp set to '%s', tsFirst set to '%s'", tsLocalEp, tsFirst); matched = true; } else if (strcasecmp("firstEp", name) == 0) { @@ -1654,7 +1656,8 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { SEp firstEp = {0}; taosGetFqdnPortFromEp(strlen(pFirstEpItem->str) == 0 ? defaultFirstEp : pFirstEpItem->str, &firstEp); snprintf(tsFirst, sizeof(tsFirst), "%s:%u", firstEp.fqdn, firstEp.port); - cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype); + + cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype, false); uInfo("localEp set to '%s', tsFirst set to '%s'", tsLocalEp, tsFirst); matched = true; } @@ -1701,7 +1704,7 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { SEp secondEp = {0}; taosGetFqdnPortFromEp(strlen(pItem->str) == 0 ? tsFirst : pItem->str, &secondEp); snprintf(tsSecond, sizeof(tsSecond), "%s:%u", secondEp.fqdn, secondEp.port); - cfgSetItem(pCfg, "secondEp", tsSecond, pItem->stype); + cfgSetItem(pCfg, "secondEp", tsSecond, pItem->stype, false); uInfo("%s set to %s", name, tsSecond); matched = true; } else if (strcasecmp("smlChildTableName", name) == 0) { @@ -1732,7 +1735,8 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { SEp firstEp = {0}; taosGetFqdnPortFromEp(strlen(pFirstEpItem->str) == 0 ? defaultFirstEp : pFirstEpItem->str, &firstEp); snprintf(tsFirst, sizeof(tsFirst), "%s:%u", firstEp.fqdn, firstEp.port); - cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype); + + cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype, false); uInfo("localEp set to '%s', tsFirst set to '%s'", tsLocalEp, tsFirst); matched = true; } else if (strcasecmp("slowLogScope", name) == 0) { @@ -1749,7 +1753,8 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { if (strcasecmp("timezone", name) == 0) { osSetTimezone(pItem->str); uInfo("%s set from %s to %s", name, tsTimezoneStr, pItem->str); - cfgSetItem(pCfg, "timezone", tsTimezoneStr, pItem->stype); + + cfgSetItem(pCfg, "timezone", tsTimezoneStr, pItem->stype, false); matched = true; } else if (strcasecmp("tempDir", name) == 0) { uInfo("%s set from %s to %s", name, tsTempDir, pItem->str); diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index 2d190edb58..cb44b7d433 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -914,7 +914,7 @@ static int32_t execAlterLocal(SAlterLocalStmt* pStmt) { return terrno; } - if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD)) { + if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD, true)) { return terrno; } diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 0d99412d1b..d05d616233 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -80,7 +80,7 @@ int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs) { int32_t size = taosArrayGetSize(pArgs); for (int32_t i = 0; i < size; ++i) { SConfigPair *pPair = taosArrayGet(pArgs, i); - if (cfgSetItem(pCfg, pPair->name, pPair->value, CFG_STYPE_ARG_LIST) != 0) { + if (cfgSetItem(pCfg, pPair->name, pPair->value, CFG_STYPE_ARG_LIST, true) != 0) { return -1; } } @@ -325,10 +325,13 @@ static int32_t cfgUpdateDebugFlagItem(SConfig *pCfg, const char *name, bool rese return 0; } -int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype) { +int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype, bool lock) { // GRANT_CFG_SET; int32_t code = 0; - taosThreadMutexLock(&pCfg->lock); + + if (lock) { + taosThreadMutexLock(&pCfg->lock); + } SConfigItem *pItem = cfgGetItem(pCfg, name); if (pItem == NULL) { @@ -381,7 +384,10 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy break; } - taosThreadMutexUnlock(&pCfg->lock); + if (lock) { + taosThreadMutexUnlock(&pCfg->lock); + } + return code; } @@ -923,7 +929,7 @@ int32_t cfgLoadFromEnvVar(SConfig *pConfig) { if (vlen3 != 0) value3[vlen3] = 0; } - code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_VAR); + code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_VAR, true); if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; if (strcasecmp(name, "dataDir") == 0) { @@ -966,7 +972,7 @@ int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { if (vlen3 != 0) value3[vlen3] = 0; } - code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_CMD); + code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_CMD, true); if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; if (strcasecmp(name, "dataDir") == 0) { @@ -1031,7 +1037,7 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { if (vlen3 != 0) value3[vlen3] = 0; } - code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_FILE); + code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_FILE, true); if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; if (strcasecmp(name, "dataDir") == 0) { @@ -1101,7 +1107,7 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { count++; } - code = cfgSetItem(pConfig, name, newValue, CFG_STYPE_CFG_FILE); + code = cfgSetItem(pConfig, name, newValue, CFG_STYPE_CFG_FILE, true); if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; } else { paGetToken(value + vlen + 1, &value2, &vlen2); @@ -1111,7 +1117,7 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { if (vlen3 != 0) value3[vlen3] = 0; } - code = cfgSetItem(pConfig, name, value, CFG_STYPE_CFG_FILE); + code = cfgSetItem(pConfig, name, value, CFG_STYPE_CFG_FILE, true); if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; } @@ -1286,7 +1292,7 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { if (vlen3 != 0) value3[vlen3] = 0; } - code = cfgSetItem(pConfig, name, value, CFG_STYPE_APOLLO_URL); + code = cfgSetItem(pConfig, name, value, CFG_STYPE_APOLLO_URL, true); if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; if (strcasecmp(name, "dataDir") == 0) {