From db1ae558609129ee4ed178e596053d5b99d42501 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 14 Nov 2020 16:20:00 +0000 Subject: [PATCH] [TD-1785]: taos_close may fail --- src/rpc/src/rpcTcp.c | 8 ++------ src/sync/src/taosTcpPool.c | 26 +++----------------------- 2 files changed, 5 insertions(+), 29 deletions(-) diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index 17314cb591..5758a7c226 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -173,16 +173,12 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread static void taosStopTcpThread(SThreadObj* pThreadObj) { // save thread into local variable and signal thread to stop pthread_t thread = pThreadObj->thread; - pThreadObj->stop = true; - if (taosComparePthread(thread, pthread_self())) { pthread_detach(pthread_self()); return; } - // at this step, pThreadObj has already been released - if (taosCheckPthreadValid(thread)) { - pthread_join(thread, NULL); - } + pThreadObj->stop = true; + pthread_join(thread, NULL); } void taosStopTcpServer(void *handle) { diff --git a/src/sync/src/taosTcpPool.c b/src/sync/src/taosTcpPool.c index 3024d7d4e3..1784dc60b7 100644 --- a/src/sync/src/taosTcpPool.c +++ b/src/sync/src/taosTcpPool.c @@ -301,31 +301,11 @@ static SThreadObj *taosGetTcpThread(SPoolObj *pPool) { } static void taosStopPoolThread(SThreadObj *pThread) { - pThread->stop = true; - - if (pThread->thread == pthread_self()) { + pthread_t thread = pThread->thread; + if (taosComparePthread(thread, pthread_self())) { pthread_detach(pthread_self()); return; } - - // save thread ID into a local variable, since pThread is freed when the thread exits - pthread_t thread = pThread->thread; - - // signal the thread to stop, try graceful method first, - // and use pthread_cancel when failed - struct epoll_event event = {.events = EPOLLIN}; - eventfd_t fd = eventfd(1, 0); - if (fd == -1) { - // failed to create eventfd, call pthread_cancel instead, which may result in data corruption - sError("failed to create eventfd since %s", strerror(errno)); - pthread_cancel(pThread->thread); - pThread->stop = true; - } else if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) { - // failed to call epoll_ctl, call pthread_cancel instead, which may result in data corruption - sError("failed to call epoll_ctl since %s", strerror(errno)); - pthread_cancel(pThread->thread); - } - + pThread->stop = true; pthread_join(thread, NULL); - if (fd >= 0) taosClose(fd); }