From 15c3945eb783ce14f5aea505a801217646dd0687 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 21 Jul 2022 12:38:49 +0000 Subject: [PATCH] fix: another concurrency problem --- source/dnode/vnode/src/tsdb/tsdbMemTable.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable.c b/source/dnode/vnode/src/tsdb/tsdbMemTable.c index 52b6e07903..fa775bb882 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable.c @@ -93,7 +93,11 @@ static int32_t tbDataPCmprFn(const void *p1, const void *p2) { } void tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) { STbData *pTbData = &(STbData){.suid = suid, .uid = uid}; - void *p = taosArraySearch(pMemTable->aTbData, &pTbData, tbDataPCmprFn, TD_EQ); + + taosRLockLatch(&pMemTable->latch); + void *p = taosArraySearch(pMemTable->aTbData, &pTbData, tbDataPCmprFn, TD_EQ); + taosRUnLockLatch(&pMemTable->latch); + *ppTbData = p ? *(STbData **)p : NULL; } @@ -363,10 +367,13 @@ static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid void *p; if (idx < 0) { - p = taosArrayPush(pMemTable->aTbData, &pTbData); - } else { - p = taosArrayInsert(pMemTable->aTbData, idx, &pTbData); + idx = taosArrayGetSize(pMemTable->aTbData); } + + taosWLockLatch(&pMemTable->latch); + p = taosArrayInsert(pMemTable->aTbData, idx, &pTbData); + taosWUnLockLatch(&pMemTable->latch); + if (p == NULL) { code = TSDB_CODE_OUT_OF_MEMORY; goto _err; -- GitLab