From be142916e6cad815c011f833417884e719a9e1f8 Mon Sep 17 00:00:00 2001 From: Jeff Tao Date: Thu, 22 Oct 2020 09:57:44 +0000 Subject: [PATCH 1/3] TD-1772 pThreadObj is freed when thread exits --- src/dnode/src/dnodeVWrite.c | 1 + src/rpc/src/rpcTcp.c | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/dnode/src/dnodeVWrite.c b/src/dnode/src/dnodeVWrite.c index e56bae0d7e..f2740bf6b8 100644 --- a/src/dnode/src/dnodeVWrite.c +++ b/src/dnode/src/dnodeVWrite.c @@ -190,6 +190,7 @@ void dnodeFreeVnodeWqueue(void *wqueue) { void dnodeSendRpcVnodeWriteRsp(void *pVnode, void *param, int32_t code) { SWriteMsg *pWrite = (SWriteMsg *)param; + if (pWrite == NULL) return; if (code < 0) pWrite->code = code; int32_t count = atomic_add_fetch_32(&pWrite->processedCount, 1); diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index 0b9bbae92e..1040877057 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -174,6 +174,10 @@ static void taosStopTcpThread(SThreadObj* pThreadObj) { pThreadObj->stop = true; eventfd_t fd = -1; + // save thread and pollFd into local variable since pThreadObj will be freed when thread exits + pthread_t thread = pThreadObj->thread; + int pollFd = pThreadObj->pollFd; + if (taosComparePthread(pThreadObj->thread, pthread_self())) { pthread_detach(pthread_self()); return; @@ -196,8 +200,9 @@ static void taosStopTcpThread(SThreadObj* pThreadObj) { } } - if (taosCheckPthreadValid(pThreadObj->thread) && pThreadObj->pollFd >= 0) { - pthread_join(pThreadObj->thread, NULL); + // at this step, pThreadObj may have been released + if (taosCheckPthreadValid(thread) && pollFd >= 0) { + pthread_join(thread, NULL); } if (fd != -1) taosCloseSocket(fd); From 0057a138eeef4440d9d63402bb11f3a86fa23c90 Mon Sep 17 00:00:00 2001 From: Jeff Tao Date: Thu, 22 Oct 2020 10:19:01 +0000 Subject: [PATCH 2/3] socket data type --- src/rpc/src/rpcTcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index 1040877057..433ffe76b5 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -176,7 +176,7 @@ static void taosStopTcpThread(SThreadObj* pThreadObj) { // save thread and pollFd into local variable since pThreadObj will be freed when thread exits pthread_t thread = pThreadObj->thread; - int pollFd = pThreadObj->pollFd; + SOCKET pollFd = pThreadObj->pollFd; if (taosComparePthread(pThreadObj->thread, pthread_self())) { pthread_detach(pthread_self()); From 4bb83933b2592ada091802ef146f735e42f31d34 Mon Sep 17 00:00:00 2001 From: Jeff Tao Date: Thu, 22 Oct 2020 13:36:54 +0000 Subject: [PATCH 3/3] remove the pollFd check --- src/rpc/src/rpcTcp.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index 433ffe76b5..bc8d360d39 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -174,16 +174,15 @@ static void taosStopTcpThread(SThreadObj* pThreadObj) { pThreadObj->stop = true; eventfd_t fd = -1; - // save thread and pollFd into local variable since pThreadObj will be freed when thread exits + // save thread into local variable since pThreadObj is freed when thread exits pthread_t thread = pThreadObj->thread; - SOCKET pollFd = pThreadObj->pollFd; if (taosComparePthread(pThreadObj->thread, pthread_self())) { pthread_detach(pthread_self()); return; } - if (taosCheckPthreadValid(pThreadObj->thread) && pThreadObj->pollFd >= 0) { + if (taosCheckPthreadValid(pThreadObj->thread)) { // signal the thread to stop, try graceful method first, // and use pthread_cancel when failed struct epoll_event event = { .events = EPOLLIN }; @@ -200,8 +199,8 @@ static void taosStopTcpThread(SThreadObj* pThreadObj) { } } - // at this step, pThreadObj may have been released - if (taosCheckPthreadValid(thread) && pollFd >= 0) { + // at this step, pThreadObj has already been released + if (taosCheckPthreadValid(thread)) { pthread_join(thread, NULL); }