Skip to content

Commit ef18811

Browse files
authored
Merge pull request #28170 from taosdata/fix/TD-32360-3.0
fix(query)[TD-32360]. Fix memory leak during tsort in exceptional scenarios
2 parents 47b50fa + f5c9bf9 commit ef18811

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

source/common/src/tdatablock.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ int32_t blockDataExtractBlock(SSDataBlock* pBlock, int32_t startIndex, int32_t r
867867

868868
code = blockDataEnsureCapacity(pDst, rowCount);
869869
if (code) {
870+
blockDataDestroy(pDst);
870871
return code;
871872
}
872873

source/libs/executor/src/tsort.c

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,43 +2391,44 @@ static int32_t createBlocksMergeSortInitialSources(SSortHandle* pHandle) {
23912391
return code;
23922392
}
23932393

2394-
static void freeSortSource(SSortSource* pSource) {
2395-
if (NULL == pSource) {
2394+
static void freeSortSource(void* p) {
2395+
SSortSource** pSource = (SSortSource**)p;
2396+
if (NULL == pSource || NULL == *pSource) {
23962397
return;
23972398
}
23982399

2399-
if (!pSource->onlyRef && pSource->param) {
2400-
taosMemoryFree(pSource->param);
2400+
if ((*pSource)->pageIdList) {
2401+
taosArrayDestroy((*pSource)->pageIdList);
24012402
}
24022403

2403-
if (!pSource->onlyRef && pSource->src.pBlock) {
2404-
blockDataDestroy(pSource->src.pBlock);
2405-
pSource->src.pBlock = NULL;
2404+
if (!(*pSource)->onlyRef) {
2405+
if ((*pSource)->param) {
2406+
taosMemoryFree((*pSource)->param);
2407+
}
2408+
if ((*pSource)->src.pBlock) {
2409+
blockDataDestroy((*pSource)->src.pBlock);
2410+
}
24062411
}
24072412

2408-
taosMemoryFree(pSource);
2413+
taosMemoryFreeClear(*pSource);
24092414
}
24102415

24112416
static int32_t createBlocksQuickSortInitialSources(SSortHandle* pHandle) {
24122417
int32_t code = 0;
2418+
int32_t lino = 0;
24132419
size_t sortBufSize = pHandle->numOfPages * pHandle->pageSize;
24142420
SSortSource** p = taosArrayGet(pHandle->pOrderedSource, 0);
24152421
if (p == NULL) {
24162422
return terrno;
24172423
}
24182424

24192425
SSortSource* pSource = *p;
2420-
2421-
taosArrayRemove(pHandle->pOrderedSource, 0);
2422-
tsortClearOrderedSource(pHandle->pOrderedSource, NULL, NULL);
2426+
size_t origSourceCount = taosArrayGetSize(pHandle->pOrderedSource);
24232427

24242428
while (1) {
24252429
SSDataBlock* pBlock = NULL;
24262430
code = pHandle->fetchfp(pSource->param, &pBlock);
2427-
if (code != 0) {
2428-
freeSortSource(pSource);
2429-
return code;
2430-
}
2431+
QUERY_CHECK_CODE(code, lino, _end);
24312432

24322433
if (pBlock == NULL) {
24332434
break;
@@ -2441,54 +2442,38 @@ static int32_t createBlocksQuickSortInitialSources(SSortHandle* pHandle) {
24412442
pHandle->numOfPages = 1024;
24422443
sortBufSize = pHandle->numOfPages * pHandle->pageSize;
24432444
code = createOneDataBlock(pBlock, false, &pHandle->pDataBlock);
2444-
if (code) {
2445-
freeSortSource(pSource);
2446-
return code;
2447-
}
2445+
QUERY_CHECK_CODE(code, lino, _end);
24482446
}
24492447

24502448
if (pHandle->beforeFp != NULL) {
24512449
pHandle->beforeFp(pBlock, pHandle->param);
24522450
}
24532451

24542452
code = blockDataMerge(pHandle->pDataBlock, pBlock);
2455-
if (code != TSDB_CODE_SUCCESS) {
2456-
freeSortSource(pSource);
2457-
return code;
2458-
}
2453+
QUERY_CHECK_CODE(code, lino, _end);
24592454

24602455
size_t size = blockDataGetSize(pHandle->pDataBlock);
24612456
if (size > sortBufSize) {
24622457
// Perform the in-memory sort and then flush data in the buffer into disk.
24632458
int64_t st = taosGetTimestampUs();
24642459
code = blockDataSort(pHandle->pDataBlock, pHandle->pSortInfo);
2465-
if (code != 0) {
2466-
freeSortSource(pSource);
2467-
return code;
2468-
}
2460+
QUERY_CHECK_CODE(code, lino, _end);
24692461

24702462
pHandle->sortElapsed += (taosGetTimestampUs() - st);
24712463

24722464
if (pHandle->pqMaxRows > 0) blockDataKeepFirstNRows(pHandle->pDataBlock, pHandle->pqMaxRows);
24732465
code = doAddToBuf(pHandle->pDataBlock, pHandle);
2474-
if (code != TSDB_CODE_SUCCESS) {
2475-
freeSortSource(pSource);
2476-
return code;
2477-
}
2466+
QUERY_CHECK_CODE(code, lino, _end);
24782467
}
24792468
}
24802469

2481-
freeSortSource(pSource);
2482-
24832470
if (pHandle->pDataBlock != NULL && pHandle->pDataBlock->info.rows > 0) {
24842471
size_t size = blockDataGetSize(pHandle->pDataBlock);
24852472

24862473
// Perform the in-memory sort and then flush data in the buffer into disk.
24872474
int64_t st = taosGetTimestampUs();
24882475
code = blockDataSort(pHandle->pDataBlock, pHandle->pSortInfo);
2489-
if (code != 0) {
2490-
return code;
2491-
}
2476+
QUERY_CHECK_CODE(code, lino, _end);
24922477

24932478
if (pHandle->pqMaxRows > 0) blockDataKeepFirstNRows(pHandle->pDataBlock, pHandle->pqMaxRows);
24942479
pHandle->sortElapsed += (taosGetTimestampUs() - st);
@@ -2501,12 +2486,16 @@ static int32_t createBlocksQuickSortInitialSources(SSortHandle* pHandle) {
25012486
pHandle->loops = 1;
25022487
pHandle->tupleHandle.rowIndex = -1;
25032488
pHandle->tupleHandle.pBlock = pHandle->pDataBlock;
2504-
return 0;
25052489
} else {
25062490
code = doAddToBuf(pHandle->pDataBlock, pHandle);
25072491
}
25082492
}
25092493

2494+
_end:
2495+
if (code != TSDB_CODE_SUCCESS) {
2496+
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2497+
}
2498+
taosArrayRemoveBatch(pHandle->pOrderedSource, 0, origSourceCount, freeSortSource);
25102499
return code;
25112500
}
25122501

0 commit comments

Comments
 (0)