From f6e76270d20f276b10bde526383afcdb13e0dc7a Mon Sep 17 00:00:00 2001 From: dongzhizhao Date: Thu, 17 Feb 2022 15:10:49 +0800 Subject: [PATCH] FIX BUG(Issue #1776): Switching pthread causes coredump when bthread local variable is destructed If the bthread local destructor specified by bthread_key_create calls bthread_mutex_lock it may cause TaskGroup::task_runner->return_keytable to switch pthreads after execution causing coredump --- src/bthread/task_group.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bthread/task_group.cpp b/src/bthread/task_group.cpp index 9f6d42fb22..f49495b9e7 100644 --- a/src/bthread/task_group.cpp +++ b/src/bthread/task_group.cpp @@ -297,9 +297,6 @@ void TaskGroup::task_runner(intptr_t skip_remained) { thread_return = e.value(); } - // Group is probably changed - g = tls_task_group; - // TODO: Save thread_return (void)thread_return; @@ -314,13 +311,16 @@ void TaskGroup::task_runner(intptr_t skip_remained) { // Clean tls variables, must be done before changing version_butex // otherwise another thread just joined this thread may not see side // effects of destructing tls variables. - KeyTable* kt = tls_bls.keytable; + KeyTable* kt = m->local_storage.keytable; if (kt != NULL) { return_keytable(m->attr.keytable_pool, kt); // After deletion: tls may be set during deletion. tls_bls.keytable = NULL; m->local_storage.keytable = NULL; // optional } + + // Group is probably changed + g = tls_task_group; // Increase the version and wake up all joiners, if resulting version // is 0, change it to 1 to make bthread_t never be 0. Any access