From 93642189636060d6cec2b3b9990725d9636edc99 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 21 May 2022 21:59:04 +0800 Subject: [PATCH] fix mem leak --- include/common/tmsg.h | 10 +++--- source/client/src/clientHb.c | 45 ++++++++++++------------ source/client/src/clientImpl.c | 7 ++-- source/client/src/clientStmt.c | 9 +++-- source/dnode/mnode/impl/src/mndProfile.c | 18 +++------- source/dnode/mnode/impl/src/mndStb.c | 3 ++ source/libs/parser/src/parInsert.c | 4 +++ source/libs/parser/src/parTokenizer.c | 1 + 8 files changed, 50 insertions(+), 47 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index bd3ca6ae8d..6811b29d47 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1987,19 +1987,16 @@ static FORCE_INLINE void tFreeClientHbReq(void* pReq) { if (req->info) { tFreeReqKvHash(req->info); taosHashCleanup(req->info); + req->info = NULL; } } int32_t tSerializeSClientHbBatchReq(void* buf, int32_t bufLen, const SClientHbBatchReq* pReq); int32_t tDeserializeSClientHbBatchReq(void* buf, int32_t bufLen, SClientHbBatchReq* pReq); -static FORCE_INLINE void tFreeClientHbBatchReq(void* pReq, bool deep) { +static FORCE_INLINE void tFreeClientHbBatchReq(void* pReq) { SClientHbBatchReq* req = (SClientHbBatchReq*)pReq; - if (deep) { - taosArrayDestroyEx(req->reqs, tFreeClientHbReq); - } else { - taosArrayDestroy(req->reqs); - } + taosArrayDestroyEx(req->reqs, tFreeClientHbReq); taosMemoryFree(pReq); } @@ -2023,6 +2020,7 @@ static FORCE_INLINE void tFreeClientHbBatchRsp(void* pRsp) { int32_t tSerializeSClientHbBatchRsp(void* buf, int32_t bufLen, const SClientHbBatchRsp* pBatchRsp); int32_t tDeserializeSClientHbBatchRsp(void* buf, int32_t bufLen, SClientHbBatchRsp* pBatchRsp); +void tFreeSClientHbBatchRsp(SClientHbBatchRsp *pBatchRsp); static FORCE_INLINE int32_t tEncodeSKv(SEncoder* pEncoder, const SKv* pKv) { if (tEncodeI32(pEncoder, pKv->key) < 0) return -1; diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 13ac43bc39..b068e13b7d 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -394,6 +394,10 @@ int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, S tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen); + if (NULL == req->info) { + req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + } + taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); return TSDB_CODE_SUCCESS; @@ -429,6 +433,10 @@ int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SCl tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen); + if (NULL == req->info) { + req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + } + taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); return TSDB_CODE_SUCCESS; @@ -463,6 +471,10 @@ int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SC tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen); + if (NULL == req->info) { + req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + } + taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); return TSDB_CODE_SUCCESS; @@ -511,16 +523,6 @@ static FORCE_INLINE void hbMgrInitHandle() { hbMgrInitMqHbHandle(); } -void hbFreeReq(void *req) { - SClientHbReq *pReq = (SClientHbReq *)req; - tFreeReqKvHash(pReq->info); -} - -void hbClearClientHbReq(SClientHbReq *pReq) { - pReq->query = NULL; - pReq->info = NULL; -} - SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { SClientHbBatchReq *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq)); if (pBatchReq == NULL) { @@ -535,6 +537,8 @@ SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { while (pIter != NULL) { SClientHbReq *pOneReq = pIter; + pOneReq = taosArrayPush(pBatchReq->reqs, pOneReq); + SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &pOneReq->connKey, sizeof(SClientHbKey)); if (info) { code = (*clientHbMgr.reqHandle[pOneReq->connKey.connType])(&pOneReq->connKey, info->param, pOneReq); @@ -544,7 +548,6 @@ SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { } } - taosArrayPush(pBatchReq->reqs, pOneReq); //hbClearClientHbReq(pOneReq); pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter); @@ -601,8 +604,8 @@ static void *hbThreadFunc(void *param) { void *buf = taosMemoryMalloc(tlen); if (buf == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; - tFreeClientHbBatchReq(pReq, false); - hbClearReqInfo(pAppHbMgr); + tFreeClientHbBatchReq(pReq); + //hbClearReqInfo(pAppHbMgr); break; } @@ -611,8 +614,8 @@ static void *hbThreadFunc(void *param) { if (pInfo == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; - tFreeClientHbBatchReq(pReq, false); - hbClearReqInfo(pAppHbMgr); + tFreeClientHbBatchReq(pReq); + //hbClearReqInfo(pAppHbMgr); taosMemoryFree(buf); break; } @@ -628,8 +631,8 @@ static void *hbThreadFunc(void *param) { int64_t transporterId = 0; SEpSet epSet = getEpSet_s(&pAppInstInfo->mgmtEp); asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo); - tFreeClientHbBatchReq(pReq, false); - hbClearReqInfo(pAppHbMgr); + tFreeClientHbBatchReq(pReq); + //hbClearReqInfo(pAppHbMgr); atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1); } @@ -721,8 +724,7 @@ void appHbMgrCleanup(void) { void *pIter = taosHashIterate(pTarget->activeInfo, NULL); while (pIter != NULL) { SClientHbReq *pOneReq = pIter; - hbFreeReq(pOneReq); - taosHashCleanup(pOneReq->info); + tFreeClientHbReq(pOneReq); pIter = taosHashIterate(pTarget->activeInfo, pIter); } taosHashCleanup(pTarget->activeInfo); @@ -782,7 +784,7 @@ int hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, SHbConnInfo * } SClientHbReq hbReq = {0}; hbReq.connKey = connKey; - hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + //hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)); @@ -823,8 +825,7 @@ int hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, in void hbDeregisterConn(SAppHbMgr *pAppHbMgr, SClientHbKey connKey) { SClientHbReq *pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey)); if (pReq) { - hbFreeReq(pReq); - taosHashCleanup(pReq->info); + tFreeClientHbReq(pReq); taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey)); } diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 8a03043a99..8c7e7cef98 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -387,7 +387,7 @@ _return: } void freeRequestRes(SRequestObj* pRequest, void* res) { - if (NULL == res) { + if (NULL == pRequest || NULL == res) { return; } @@ -435,12 +435,13 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, int32_t code if (NULL != pRequest && TSDB_CODE_SUCCESS != code) { pRequest->code = terrno; - freeRequestRes(pRequest, pRes); - pRes = NULL; } if (res) { *res = pRes; + } else { + freeRequestRes(pRequest, pRes); + pRes = NULL; } return pRequest; diff --git a/source/client/src/clientStmt.c b/source/client/src/clientStmt.c index a9b4e234e8..1746858482 100644 --- a/source/client/src/clientStmt.c +++ b/source/client/src/clientStmt.c @@ -800,6 +800,7 @@ int stmtExec(TAOS_STMT* stmt) { if (code) { pStmt->exec.pRequest->code = code; } else { + tFreeSSubmitRsp(pRsp); STMT_ERR_RET(stmtResetStmt(pStmt)); STMT_ERR_RET(TSDB_CODE_NEED_RETRY); } @@ -817,11 +818,13 @@ _return: if (TSDB_CODE_SUCCESS == code && autoCreateTbl) { if (NULL == pRsp) { tscError("no submit resp got for auto create table"); - STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR); + code = TSDB_CODE_TSC_APP_ERROR; + } else { + code = stmtUpdateTableUid(pStmt, pRsp); } - - STMT_ERR_RET(stmtUpdateTableUid(pStmt, pRsp)); } + + tFreeSSubmitRsp(pRsp); ++pStmt->sql.runTimes; diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index cd57e76b2c..b9ac82d890 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -128,7 +128,8 @@ static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType } static void mndFreeConn(SConnObj *pConn) { - taosMemoryFreeClear(pConn->pQueries); + taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc); + mTrace("conn:%u, is destroyed, data:%p", pConn->id, pConn); } @@ -396,6 +397,7 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb if (NULL == hbRsp.info) { mError("taosArrayInit %d rsp kv failed", kvNum); terrno = TSDB_CODE_OUT_OF_MEMORY; + tFreeClientHbRsp(&hbRsp); return -1; } @@ -453,6 +455,7 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) { SClientHbBatchReq batchReq = {0}; if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) { + taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq); terrno = TSDB_CODE_INVALID_MSG; return -1; } @@ -479,18 +482,7 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) { void *buf = rpcMallocCont(tlen); tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp); - int32_t rspNum = (int32_t)taosArrayGetSize(batchRsp.rsps); - for (int32_t i = 0; i < rspNum; ++i) { - SClientHbRsp *rsp = taosArrayGet(batchRsp.rsps, i); - int32_t kvNum = (rsp->info) ? taosArrayGetSize(rsp->info) : 0; - for (int32_t n = 0; n < kvNum; ++n) { - SKv *kv = taosArrayGet(rsp->info, n); - taosMemoryFreeClear(kv->value); - } - taosArrayDestroy(rsp->info); - } - - taosArrayDestroy(batchRsp.rsps); + tFreeClientHbBatchRsp(&batchRsp); pReq->info.rspLen = tlen; pReq->info.rsp = buf; diff --git a/source/dnode/mnode/impl/src/mndStb.c b/source/dnode/mnode/impl/src/mndStb.c index 7485510bc6..4ad92a5416 100644 --- a/source/dnode/mnode/impl/src/mndStb.c +++ b/source/dnode/mnode/impl/src/mndStb.c @@ -1642,6 +1642,8 @@ int32_t mndValidateStbInfo(SMnode *pMnode, SSTableMetaVersion *pStbVersions, int if (pStbVersion->sversion != metaRsp.sversion) { taosArrayPush(batchMetaRsp.pArray, &metaRsp); + } else { + tFreeSTableMetaRsp(&metaRsp); } } @@ -1660,6 +1662,7 @@ int32_t mndValidateStbInfo(SMnode *pMnode, SSTableMetaVersion *pStbVersions, int } tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp); + tFreeSTableMetaBatchRsp(&batchMetaRsp); *ppRsp = pRsp; *pRspLen = rspLen; return 0; diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index b452950624..6847d680d2 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -1085,6 +1085,10 @@ static int32_t parseInsertBody(SInsertParseContext* pCxt) { // no data in the sql string anymore. if (sToken.n == 0) { + if (sToken.type && pCxt->pSql[0]) { + return buildSyntaxErrMsg(&pCxt->msg, "invalid charactor in SQL", sToken.z); + } + if (0 == pCxt->totalNum && (!TSDB_QUERY_HAS_TYPE(pCxt->pOutput->insertType, TSDB_QUERY_TYPE_STMT_INSERT))) { return buildInvalidOperationMsg(&pCxt->msg, "no data in sql"); } diff --git a/source/libs/parser/src/parTokenizer.c b/source/libs/parser/src/parTokenizer.c index abc7ddb17f..8fb9780f8a 100644 --- a/source/libs/parser/src/parTokenizer.c +++ b/source/libs/parser/src/parTokenizer.c @@ -704,6 +704,7 @@ SToken tStrGetToken(const char* str, int32_t* i, bool isPrevOptr) { if (t0.type == TK_NK_SEMI) { t0.n = 0; + t0.type = 0; return t0; } -- GitLab