From fc544705feaa5c6c59aad2ccbd204122a1d503f9 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Mon, 8 Jul 2024 09:12:50 +0800 Subject: [PATCH 1/6] fix:[TD-30915]tmq exit elegantly --- source/client/inc/clientInt.h | 2 ++ source/client/src/clientMain.c | 5 +++-- source/client/src/clientTmq.c | 25 ++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 7a84215e12..0d22257433 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -454,6 +454,8 @@ enum { void sqlReqLog(int64_t rid, bool killed, int32_t code, int8_t type); +void tmqMgmtClose(void); + #ifdef __cplusplus } #endif diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index 56f89ffba6..d0bd1ec650 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -57,8 +57,6 @@ void taos_cleanup(void) { } monitorClose(); - taosHashCleanup(appInfo.pInstMap); - taosHashCleanup(appInfo.pInstMapByClusterId); tscStopCrashReport(); hbMgrCleanUp(); @@ -88,6 +86,9 @@ void taos_cleanup(void) { tscInfo("all local resources released"); taosCleanupCfg(); taosCloseLog(); + tmqMgmtClose(); + taosHashCleanup(appInfo.pInstMap); + taosHashCleanup(appInfo.pInstMapByClusterId); } static setConfRet taos_set_config_imp(const char *config) { diff --git a/source/client/src/clientTmq.c b/source/client/src/clientTmq.c index 21d1a528da..c448f115bb 100644 --- a/source/client/src/clientTmq.c +++ b/source/client/src/clientTmq.c @@ -29,7 +29,6 @@ #define DEFAULT_HEARTBEAT_INTERVAL 3000 struct SMqMgmt { - int8_t inited; tmr_h timer; int32_t rsetId; }; @@ -1066,6 +1065,18 @@ void tmqFreeImpl(void* handle) { taos_close_internal(tmq->pTscObj); taosMemoryFree(tmq); + if(tmq->commitTimer) { + taosTmrStopA(tmq->commitTimer); + tmq->commitTimer = NULL; + } + if(tmq->epTimer) { + taosTmrStopA(tmq->epTimer); + tmq->epTimer = NULL; + } + if(tmq->hbLiveTimer) { + taosTmrStopA(tmq->hbLiveTimer); + tmq->hbLiveTimer = NULL; + } tscDebug("consumer:0x%" PRIx64 " closed", id); } @@ -1083,6 +1094,18 @@ static void tmqMgmtInit(void) { } } +void tmqMgmtClose(void) { + if (tmqMgmt.timer) { + taosTmrCleanUp(tmqMgmt.timer); + tmqMgmt.timer = NULL; + } + + if (tmqMgmt.rsetId >= 0) { + taosCloseRef(tmqMgmt.rsetId); + tmqMgmt.rsetId = -1; + } +} + #define SET_ERROR_MSG_TMQ(MSG) \ if (errstr != NULL) snprintf(errstr, errstrLen, MSG); From 24b3bcaa9f22997d89d64aff0607a3b0b9e52b1c Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Mon, 8 Jul 2024 09:17:25 +0800 Subject: [PATCH 2/6] fix:[TD-30915]tmq exit elegantly --- source/client/src/clientMain.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index d0bd1ec650..61b7afc6b1 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -83,12 +83,13 @@ void taos_cleanup(void) { taosConvDestroy(); - tscInfo("all local resources released"); - taosCleanupCfg(); - taosCloseLog(); tmqMgmtClose(); taosHashCleanup(appInfo.pInstMap); taosHashCleanup(appInfo.pInstMapByClusterId); + + tscInfo("all local resources released"); + taosCleanupCfg(); + taosCloseLog(); } static setConfRet taos_set_config_imp(const char *config) { From e14d11c78a445ec724ee50b24722bc0144ffa5fc Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Mon, 8 Jul 2024 14:28:27 +0800 Subject: [PATCH 3/6] fix:[TD-30915]tmq exit elegantly --- source/client/src/clientTmq.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/client/src/clientTmq.c b/source/client/src/clientTmq.c index c448f115bb..c9f567b890 100644 --- a/source/client/src/clientTmq.c +++ b/source/client/src/clientTmq.c @@ -1063,7 +1063,6 @@ void tmqFreeImpl(void* handle) { taosArrayDestroyEx(tmq->clientTopics, freeClientVgImpl); taos_close_internal(tmq->pTscObj); - taosMemoryFree(tmq); if(tmq->commitTimer) { taosTmrStopA(tmq->commitTimer); @@ -1077,6 +1076,8 @@ void tmqFreeImpl(void* handle) { taosTmrStopA(tmq->hbLiveTimer); tmq->hbLiveTimer = NULL; } + taosMemoryFree(tmq); + tscDebug("consumer:0x%" PRIx64 " closed", id); } From 2ce9d05f7526ca564b39cba5fa7143320bcfbfce Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Mon, 8 Jul 2024 15:33:04 +0800 Subject: [PATCH 4/6] fix:[TD-30915]tmq exit elegantly --- source/client/src/clientTmq.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/source/client/src/clientTmq.c b/source/client/src/clientTmq.c index c9f567b890..1c5d085aaf 100644 --- a/source/client/src/clientTmq.c +++ b/source/client/src/clientTmq.c @@ -1065,16 +1065,13 @@ void tmqFreeImpl(void* handle) { taos_close_internal(tmq->pTscObj); if(tmq->commitTimer) { - taosTmrStopA(tmq->commitTimer); - tmq->commitTimer = NULL; + taosTmrStopA(&tmq->commitTimer); } if(tmq->epTimer) { - taosTmrStopA(tmq->epTimer); - tmq->epTimer = NULL; + taosTmrStopA(&tmq->epTimer); } if(tmq->hbLiveTimer) { - taosTmrStopA(tmq->hbLiveTimer); - tmq->hbLiveTimer = NULL; + taosTmrStopA(&tmq->hbLiveTimer); } taosMemoryFree(tmq); From 020900e35935ec87a8e31fbe915da18b33267bac Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 9 Jul 2024 18:39:54 +0800 Subject: [PATCH 5/6] fix:check if queue is closed before send data to queue in monitor --- source/client/src/clientMonitor.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/source/client/src/clientMonitor.c b/source/client/src/clientMonitor.c index d1a9897caa..ae2a57ba97 100644 --- a/source/client/src/clientMonitor.c +++ b/source/client/src/clientMonitor.c @@ -682,7 +682,7 @@ static void* monitorThreadFunc(void *param){ tscDebug("monitorThreadFunc start"); int64_t quitTime = 0; while (1) { - if (slowLogFlag > 0) { + if (atomic_load_32(&slowLogFlag) > 0 > 0) { if(quitCnt == 0){ monitorSendAllSlowLogAtQuit(); if(quitCnt == 0){ @@ -727,10 +727,7 @@ static void* monitorThreadFunc(void *param){ } tsem2_timewait(&monitorSem, 100); } - - taosCloseQueue(monitorQueue); - tsem2_destroy(&monitorSem); - slowLogFlag = -2; + atomic_store_32(&slowLogFlag, -2); return NULL; } @@ -826,10 +823,16 @@ void monitorClose() { taosHashCleanup(monitorCounterHash); taosHashCleanup(monitorSlowLogHash); taosTmrCleanUp(monitorTimer); + taosCloseQueue(monitorQueue); + tsem2_destroy(&monitorSem); taosWUnLockLatch(&monitorLock); } int32_t monitorPutData2MonitorQueue(MonitorSlowLogData data){ + if (atomic_load_32(&slowLogFlag) == -2) { + tscError("[monitor] slow log thread is exiting"); + return -1; + } MonitorSlowLogData* slowLogData = taosAllocateQitem(sizeof(MonitorSlowLogData), DEF_QITEM, 0); if (slowLogData == NULL) { tscError("[monitor] failed to allocate slow log data"); From 1e156c9ce13998d87ca7d15fbb4dbbc624ff2732 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 10 Jul 2024 18:58:06 +0800 Subject: [PATCH 6/6] fix:cleanup scheduler timer & remove destroy global variables to avoid heap use after free --- source/client/src/clientMain.c | 2 -- source/libs/scheduler/src/scheduler.c | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index 61b7afc6b1..f65edc103a 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -84,8 +84,6 @@ void taos_cleanup(void) { taosConvDestroy(); tmqMgmtClose(); - taosHashCleanup(appInfo.pInstMap); - taosHashCleanup(appInfo.pInstMapByClusterId); tscInfo("all local resources released"); taosCleanupCfg(); diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 795c21c234..fc92be8214 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -201,6 +201,7 @@ void schedulerDestroy(void) { } SCH_UNLOCK(SCH_WRITE, &schMgmt.hbLock); + taosTmrCleanUp(schMgmt.timer); qWorkerDestroy(&schMgmt.queryMgmt); schMgmt.queryMgmt = NULL; }