未验证 提交 49bfa0dd 编写于 作者: S slguan 提交者: GitHub

Merge pull request #1619 from taosdata/hotfix/rpcclose

remove the memory leak for timer module when it cleans up
......@@ -227,6 +227,11 @@ int taosOpenUDServerSocket(char *ip, uint16_t port) {
return sockFd;
}
static void taosDeleteTimer(void *tharg) {
timer_t *pTimer = tharg;
timer_delete(*pTimer);
}
void *taosProcessAlarmSignal(void *tharg) {
// Block the signal
sigset_t sigset;
......@@ -235,7 +240,7 @@ void *taosProcessAlarmSignal(void *tharg) {
sigprocmask(SIG_BLOCK, &sigset, NULL);
void (*callback)(int) = tharg;
timer_t timerId;
static timer_t timerId;
struct sigevent sevent = {0};
#ifdef _ALPINE
......@@ -252,6 +257,8 @@ void *taosProcessAlarmSignal(void *tharg) {
tmrError("Failed to create timer");
}
pthread_cleanup_push(taosDeleteTimer, &timerId);
struct itimerspec ts;
ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 1000000 * MSECONDS_PER_TICK;
......@@ -273,6 +280,8 @@ void *taosProcessAlarmSignal(void *tharg) {
callback(0);
}
pthread_cleanup_pop(1);
return NULL;
}
......@@ -282,13 +291,15 @@ int taosInitTimer(void (*callback)(int), int ms) {
pthread_attr_t tattr;
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback) != 0) {
int code = pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback);
pthread_detach(thread);
pthread_attr_destroy(&tattr);
if (code != 0) {
tmrError("failed to create timer thread");
return -1;
}
pthread_attr_destroy(&tattr);
return 0;
return thread;
}
ssize_t tread(int fd, void *buf, size_t count) {
......
......@@ -302,6 +302,7 @@ void rpcClose(void *param) {
tfree(pRpc->connList);
pthread_mutex_destroy(&pRpc->mutex);
tTrace("%s RPC is closed", pRpc->label);
tfree(pRpc);
}
......
......@@ -84,6 +84,8 @@ static tmr_ctrl_t* tmrCtrls;
static tmr_ctrl_t* unusedTmrCtrl = NULL;
static void* tmrQhandle;
static int numOfTmrCtrl = 0;
//static void* tmrContext = NULL;
static int athread = 0;
int taosTmrThreads = 1;
......@@ -517,7 +519,7 @@ static void taosTmrModuleInit(void) {
}
tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr");
taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK);
athread = taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK);
tmrTrace("timer module is initialized, number of threads: %d", taosTmrThreads);
}
......@@ -558,4 +560,22 @@ void taosTmrCleanUp(void* handle) {
numOfTmrCtrl--;
unusedTmrCtrl = ctrl;
pthread_mutex_unlock(&tmrCtrlMutex);
if (numOfTmrCtrl <=0) {
pthread_cancel(athread);
for (int i = 0; i < tListLen(wheels); i++) {
time_wheel_t* wheel = wheels + i;
pthread_mutex_destroy(&wheel->mutex);
free(wheel->slots);
}
pthread_mutex_destroy(&tmrCtrlMutex);
free(timerMap.slots);
free(tmrCtrls);
taosCleanUpScheduler(tmrQhandle);
tmrModuleInit = PTHREAD_ONCE_INIT;
tmrTrace("timer module is cleaned up");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册