• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5001

24 Mar 2026 01:11AM UTC coverage: 72.254% (-0.05%) from 72.307%
#5001

push

travis-ci

web-flow
fix: possible memory leak in tdb; (#34872)

1 of 20 new or added lines in 2 files covered. (5.0%)

770 existing lines in 143 files now uncovered.

253287 of 350550 relevant lines covered (72.25%)

130300301.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.59
/source/libs/executor/src/querytask.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "filter.h"
17
#include "function.h"
18
#include "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

24
#include "tdatablock.h"
25
#include "tmsg.h"
26

27
#include "executorInt.h"
28
#include "index.h"
29
#include "operator.h"
30
#include "query.h"
31
#include "querytask.h"
32
#include "storageapi.h"
33
#include "thash.h"
34
#include "ttypes.h"
35
#include "tref.h"
36

37
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
38

39
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
349,194,893✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
349,194,893✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
349,194,893✔
46
  if (p == NULL) {
349,188,375✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
349,188,375✔
51
  p->cost.created = taosGetTimestampUs();
349,199,780✔
52

53
  p->execModel = model;
349,180,858✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
349,187,637✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
349,169,447✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
349,157,254✔
57
    doDestroyTask(p);
20,425✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
349,150,665✔
62
  taosInitRWLatch(&p->lock);
349,178,754✔
63

64
  p->id.vgId = vgId;
349,155,735✔
65
  p->id.queryId = queryId;
349,154,933✔
66
  p->id.taskId = taskId;
349,170,486✔
67
  p->id.str = taosMemoryMalloc(64);
349,183,129✔
68
  if (p->id.str == NULL) {
349,122,985✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
349,157,272✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
349,151,550✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
349,083,441✔
UNCOV
76
    doDestroyTask(p);
×
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
349,127,191✔
81
  return TSDB_CODE_SUCCESS;
349,116,640✔
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
16,530✔
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,781,539,052✔
87

88
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
97,057✔
89
  pTaskInfo->code = rspCode;
97,057✔
90
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
97,057✔
91
}
97,057✔
92

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,089,012,591✔
94
  if (status == TASK_NOT_COMPLETED) {
1,089,012,591✔
95
    pTaskInfo->status = status;
349,380,476✔
96
  } else {
97
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
98
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
739,632,115✔
99
    pTaskInfo->status |= status;
739,641,327✔
100
  }
101
}
1,089,032,904✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
39,789,593✔
105
  int32_t code = 0, lino = 0;
39,789,593✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
39,789,593✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
39,788,512✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
39,789,583✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
39,793,430✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
39,791,225✔
114
  ctx->taskId = pTaskInfo->id.taskId;
39,792,348✔
115
  ctx->idStr = pTaskInfo->id.str;
39,793,971✔
116
  ctx->pTaskInfo = pTaskInfo;
39,790,123✔
117
  ctx->subEndPoints = *subEndPoints;
39,791,226✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
39,790,103✔
119
  ctx->isStream = pTaskInfo && IS_STREAM_MODE(pTaskInfo);
39,797,276✔
120

121
  *subEndPoints = NULL;
39,793,980✔
122
  
123
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
39,792,868✔
124
  if (NULL == ctx->subResNodes) {
39,788,502✔
125
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
126
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
127
  }
128
  
129
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
39,791,236✔
130

131
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
39,790,684✔
132
  if (refId < 0) {
39,791,788✔
133
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
134
    TAOS_CHECK_EXIT(terrno);
×
135
  }
136
  
137
  ctx->subJobRefId = refId;
39,791,788✔
138

139
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
39,791,247✔
140

141
_exit:
39,314,801✔
142

143
  if (code) {
39,789,604✔
144
    destroySubJobCtx(pTaskInfo->pSubJobCtx);
×
145
    pTaskInfo->pSubJobCtx = NULL;
×
146
    
147
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
148
  }
149

150
  return code;
39,787,412✔
151
}
152

153

154

155
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
349,076,454✔
156
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints, bool enableExplain) {
157
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
349,076,454✔
158
  if (*pTaskInfo == NULL || code != 0) {
348,987,496✔
159
    nodesDestroyNode((SNode*)pPlan);
76✔
160
    return code;
×
161
  }
162

163
  (*pTaskInfo)->pSubplan = pPlan;
349,015,202✔
164

165
  if (NULL != sql) {
348,988,621✔
166
    (*pTaskInfo)->sql = taosStrdup(sql);
324,882,935✔
167
    if (NULL == (*pTaskInfo)->sql) {
324,908,841✔
168
      code = terrno;
×
169
      doDestroyTask(*pTaskInfo);
×
170
      (*pTaskInfo) = NULL;
×
171
      return code;
×
172
    }
173
  }
174

175
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
348,967,717✔
176
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
348,993,457✔
177
  (*pTaskInfo)->enableExplain = enableExplain;
348,984,861✔
178

179
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
348,974,288✔
180
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
39,790,685✔
181
    if (code != TSDB_CODE_SUCCESS) {
39,786,309✔
182
      doDestroyTask(*pTaskInfo);
×
183
      (*pTaskInfo) = NULL;
×
184
      return code;
×
185
    }
186
  }
187
  
188
  setTaskScalarExtraInfo(*pTaskInfo);
349,016,434✔
189
  
190
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
348,999,845✔
191
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
348,942,430✔
192

193
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
348,806,924✔
194
    doDestroyTask(*pTaskInfo);
733,070✔
195
    (*pTaskInfo) = NULL;
470,441✔
196
  }
197
  return code;
348,650,627✔
198
}
199

200
void cleanupQueriedTableScanInfo(void* p) {
261,484,112✔
201
  SSchemaInfo* pSchemaInfo = p;
261,484,112✔
202

203
  taosMemoryFreeClear(pSchemaInfo->dbname);
261,484,112✔
204
  taosMemoryFreeClear(pSchemaInfo->tablename);
261,478,142✔
205
  tDeleteSchemaWrapper(pSchemaInfo->sw);
261,470,534✔
206
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
261,472,715✔
207
}
261,468,765✔
208

209
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
261,549,699✔
210
                                   SExecTaskInfo* pTaskInfo) {
211
  SMetaReader mr = {0};
261,549,699✔
212
  if (pHandle == NULL) {
261,552,364✔
213
    return TSDB_CODE_INVALID_PARA;
×
214
  }
215

216
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
261,552,364✔
217

218
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
261,548,403✔
219
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
261,559,671✔
220
  if (code != TSDB_CODE_SUCCESS) {
261,285,968✔
221
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
222
           GET_TASKID(pTaskInfo));
223

224
    pAPI->metaReaderFn.clearReader(&mr);
×
225
    return code;
×
226
  }
227

228
  SSchemaInfo schemaInfo = {0};
261,285,968✔
229

230
  schemaInfo.tablename = taosStrdup(mr.me.name);
261,318,509✔
231
  schemaInfo.dbname = taosStrdup(dbName);
261,369,048✔
232
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
261,431,422✔
233
    pAPI->metaReaderFn.clearReader(&mr);
146,818✔
234
    cleanupQueriedTableScanInfo(&schemaInfo);
×
235
    return terrno;
×
236
  }
237

238
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
261,284,629✔
239
    schemaInfo.rversion = mr.me.colRef.version;
294,808✔
240
  }
241

242
  if (mr.me.type == TSDB_SUPER_TABLE) {
261,284,629✔
243
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
114,480,093✔
244
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
114,480,093✔
245
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
146,880,166✔
246
    tDecoderClear(&mr.coder);
103,622,479✔
247

248
    tb_uid_t suid = mr.me.ctbEntry.suid;
103,613,091✔
249
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
103,613,091✔
250
    if (code != TSDB_CODE_SUCCESS) {
103,613,181✔
251
      pAPI->metaReaderFn.clearReader(&mr);
×
252
      cleanupQueriedTableScanInfo(&schemaInfo);
×
253
      return code;
×
254
    }
255

256
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
103,610,300✔
257
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
103,610,300✔
258
  } else {
259
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
43,272,504✔
260
  }
261

262
  pAPI->metaReaderFn.clearReader(&mr);
261,362,897✔
263

264
  if (schemaInfo.sw == NULL) {
261,248,276✔
265
    cleanupQueriedTableScanInfo(&schemaInfo);
×
266
    return terrno;
×
267
  }
268

269
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
261,248,276✔
270
  if (schemaInfo.qsw == NULL) {
261,332,423✔
271
    cleanupQueriedTableScanInfo(&schemaInfo);
×
272
    return terrno;
×
273
  }
274

275
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
261,332,423✔
276
  if (p == NULL) {
261,412,990✔
277
    cleanupQueriedTableScanInfo(&schemaInfo);
×
278
    return terrno;
×
279
  }
280

281
  return code;
261,412,990✔
282
}
283

284
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
261,333,733✔
285
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
261,333,733✔
286
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
261,417,708✔
287

288
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
261,273,132✔
289
  if (pqSw == NULL) {
261,235,565✔
290
    return NULL;
×
291
  }
292

293
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
261,235,565✔
294
  if (pqSw->pSchema == NULL) {
261,049,277✔
295
    taosMemoryFree(pqSw);
×
296
    return NULL;
×
297
  }
298

299
  for (int32_t i = 0; i < numOfCols; ++i) {
1,169,186,537✔
300
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
907,692,656✔
301
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
907,833,814✔
302

303
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
907,909,147✔
304
    pSchema->colId = pColNode->colId;
907,817,205✔
305
    pSchema->type = pColNode->node.resType.type;
907,910,842✔
306
    pSchema->bytes = pColNode->node.resType.bytes;
907,857,505✔
307
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
907,915,315✔
308
  }
309

310
  // this the tags and pseudo function columns, we only keep the tag columns
311
  for (int32_t i = 0; i < numOfTags; ++i) {
407,102,059✔
312
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
145,597,205✔
313

314
    int32_t type = nodeType(pNode->pExpr);
145,627,223✔
315
    if (type == QUERY_NODE_COLUMN) {
145,652,172✔
316
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
107,063,735✔
317

318
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
107,040,694✔
319
      pSchema->colId = pColNode->colId;
107,011,870✔
320
      pSchema->type = pColNode->node.resType.type;
106,993,552✔
321
      pSchema->bytes = pColNode->node.resType.bytes;
106,940,696✔
322
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
106,983,511✔
323
    }
324
  }
325

326
  return pqSw;
261,504,854✔
327
}
328

329
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
349,094,629✔
330
  tDeleteSchemaWrapper(pTmqInfo->schema);
349,094,629✔
331
  tOffsetDestroy(&pTmqInfo->currentOffset);
349,115,430✔
332
}
348,993,166✔
333

334
static void freeBlock(void* pParam) {
590,135,030✔
335
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
590,135,030✔
336
  blockDataDestroy(pBlock);
590,149,726✔
337
}
590,142,560✔
338

339

340
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
39,793,440✔
341
  if (pCtx->transporterId > 0) {
39,793,440✔
342
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
6,061✔
343
    if (ret != 0) {
6,061✔
344
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
345
    }
346
    pCtx->transporterId = -1;
6,061✔
347
  }
348

349
  if (pCtx->subEndPoints != NULL) {
39,796,735✔
350
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
39,796,726✔
351
    if (size > 0) {
39,790,718✔
352
      int32_t code = tsem_destroy(&pCtx->ready);
39,790,167✔
353
      if (code != TSDB_CODE_SUCCESS) {
39,791,766✔
354
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
355
      }
356
      taosArrayDestroy(pCtx->subResNodes);
39,791,766✔
357
    }
358
    if (pCtx->isStream) {
39,790,135✔
359
      taosArrayDestroyP(pCtx->subEndPoints, (FDelete)nodesDestroyNode);
258,404✔
360
    } else {
361
      taosArrayDestroyP(pCtx->subEndPoints, NULL);
39,531,731✔
362
    }
363
    pCtx->subEndPoints = NULL;
39,786,278✔
364
  }
365
  
366
  taosMemoryFreeClear(pCtx);  
39,786,829✔
367
}
39,790,157✔
368

369
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
349,196,855✔
370
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
349,196,855✔
371
  destroyOperator(pTaskInfo->pRoot);
349,197,907✔
372
  pTaskInfo->pRoot = NULL;
349,122,339✔
373

374
  if (pTaskInfo->pSubJobCtx) {
349,149,365✔
375
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
39,792,328✔
376
    if (code != TSDB_CODE_SUCCESS) {
39,796,185✔
377
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
378
    }
379
  }
380

381
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
349,168,844✔
382
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
349,094,354✔
383

384
  if (!pTaskInfo->localFetch.localExec) {
348,983,503✔
385
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
349,017,280✔
386
    pTaskInfo->pSubplan = NULL;
349,136,150✔
387
  }
388

389
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
349,143,567✔
390
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
349,143,548✔
391
  if (!pTaskInfo->paramSet) {
349,130,653✔
392
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
341,636,394✔
393
    pTaskInfo->pOpParam = NULL;
341,575,096✔
394
  }
395
  taosMemoryFreeClear(pTaskInfo->sql);
349,097,708✔
396
  taosMemoryFreeClear(pTaskInfo->id.str);
349,087,482✔
397
  taosMemoryFreeClear(pTaskInfo);
349,111,335✔
398
}
349,070,251✔
399

400
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
363,083,479✔
401
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
363,083,479✔
402
  if (ret < 0) {
363,083,479✔
403
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
404
  }
405
}
363,083,479✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc