From a42d498169e69202f485f97e59f5db6f0461fc29 Mon Sep 17 00:00:00 2001 From: kailixu Date: Sat, 4 Jan 2025 20:28:16 +0800 Subject: [PATCH 1/2] fix: eliminate the risk of deadlock when switching logs --- source/util/src/tlog.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 4df5b322a2..4a9f2a9937 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -411,9 +411,7 @@ static OldFileKeeper *taosOpenNewFile() { TdFilePtr pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { - tsLogObj.openInProgress = 0; - tsLogObj.lines = tsNumOfLogLines - 1000; - uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); + uError("open new log file fail! reason:%s, reuse lastlog", tstrerror(terrno)); return NULL; } @@ -425,7 +423,6 @@ static OldFileKeeper *taosOpenNewFile() { TdFilePtr pOldFile = tsLogObj.logHandle->pFile; tsLogObj.logHandle->pFile = pFile; tsLogObj.lines = 0; - tsLogObj.openInProgress = 0; OldFileKeeper *oldFileKeeper = taosMemoryMalloc(sizeof(OldFileKeeper)); if (oldFileKeeper == NULL) { uError("create old log keep info faild! mem is not enough."); @@ -468,7 +465,9 @@ static int32_t taosOpenNewLogFile() { OldFileKeeper *oldFileKeeper = taosOpenNewFile(); if (!oldFileKeeper) { + tsLogObj.openInProgress = 0; TAOS_UNUSED(taosThreadMutexUnlock(&tsLogObj.logMutex)); + (void)taosThreadAttrDestroy(&attr); return terrno; } if (taosThreadCreate(&thread, &attr, taosThreadToCloseOldFile, oldFileKeeper) != 0) { @@ -476,6 +475,7 @@ static int32_t taosOpenNewLogFile() { taosMemoryFreeClear(oldFileKeeper); } (void)taosThreadAttrDestroy(&attr); + tsLogObj.openInProgress = 0; } (void)taosThreadMutexUnlock(&tsLogObj.logMutex); From f2735f43bc3e51f367b6633dd68628d6c6530659 Mon Sep 17 00:00:00 2001 From: kailixu Date: Sat, 4 Jan 2025 23:40:58 +0800 Subject: [PATCH 2/2] fix: eliminate the risk of deadlock when switching logs --- source/util/src/tlog.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 4a9f2a9937..90753ae7e8 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -411,7 +411,9 @@ static OldFileKeeper *taosOpenNewFile() { TdFilePtr pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { - uError("open new log file fail! reason:%s, reuse lastlog", tstrerror(terrno)); + tsLogObj.flag ^= 1; + tsLogObj.lines = tsNumOfLogLines - 1000; + uError("open new log file %s fail! reason:%s, reuse lastlog", name, tstrerror(terrno)); return NULL; } @@ -728,10 +730,7 @@ static inline void taosPrintLogImp(ELogLevel level, int32_t dflag, const char *b if (tsNumOfLogLines > 0) { TAOS_UNUSED(atomic_add_fetch_32(&tsLogObj.lines, 1)); if ((tsLogObj.lines > tsNumOfLogLines) && (tsLogObj.openInProgress == 0)) { - int32_t code = taosOpenNewLogFile(); - if (code != 0) { - uError("failed to open new log file, reason:%s", tstrerror(code)); - } + TAOS_UNUSED(taosOpenNewLogFile()); } } }