From 5d7af856f769a77b398de64597458eca252ef43c Mon Sep 17 00:00:00 2001 From: "rafal@quant.(none)" <> Date: Fri, 8 Dec 2006 11:41:12 +0100 Subject: [PATCH 1/2] BUG#24507 (rpl_log.test crash slave): The problem was located to lie inside current NPTL pthread_exit() implementation. Race conditions in this code can lead to segmentation fault. Hovewer, this can happen only in a race between first thread calling pthread_exit() and other threads. Workaround implemented in this patch spawns a dummy thread, which exits immediately, during thread lib initialization. This will exclude segment violations when further threads exit. --- include/my_pthread.h | 20 ++++++++++++++++++++ mysys/my_thr_init.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/my_pthread.h b/include/my_pthread.h index 3e4388413e0..8051d75c59e 100644 --- a/include/my_pthread.h +++ b/include/my_pthread.h @@ -31,6 +31,26 @@ extern "C" { #define EXTERNC #endif /* __cplusplus */ +/* + BUG#24507: Race conditions inside current NPTL pthread_exit() implementation. + + If macro NPTL_PTHREAD_EXIT_HACK is defined then a hack described in the bug + report will be implemented inside my_thread_global_init() in my_thr_init.c. + + This amounts to spawning a dummy thread which does nothing but executes + pthread_exit(0). + + This bug is fixed in version 2.5 of glibc library. + + TODO: Remove this code when fixed versions of glibc6 are in common use. + */ + +#if defined(TARGET_OS_LINUX) && defined(HAVE_NPTL) && + defined(__GLIBC__) && ( __GLIBC__ < 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ < 5 ) +#define NPTL_PTHREAD_EXIT_BUG 1 +#endif + + #if defined(__WIN__) || defined(OS2) #ifdef OS2 diff --git a/mysys/my_thr_init.c b/mysys/my_thr_init.c index 4d23d01cd82..6663af986dd 100644 --- a/mysys/my_thr_init.c +++ b/mysys/my_thr_init.c @@ -44,6 +44,23 @@ pthread_mutexattr_t my_fast_mutexattr; pthread_mutexattr_t my_errorcheck_mutexattr; #endif +#ifdef NPTL_PTHREAD_EXIT_BUG /* see my_pthread.h */ + +/* + Dummy thread spawned in my_thread_global_init() below to avoid + race conditions in NPTL pthread_exit code. +*/ + +static +pthread_handler_t nptl_pthread_exit_hack_handler(void *arg) +{ + /* Do nothing! */ + pthread_exit(0); + return 0; +} + +#endif + /* initialize thread environment @@ -62,6 +79,28 @@ my_bool my_thread_global_init(void) fprintf(stderr,"Can't initialize threads: error %d\n",errno); return 1; } + +#ifdef NPTL_PTHREAD_EXIT_BUG + +/* + BUG#24507: Race conditions inside current NPTL pthread_exit() implementation. + + To avoid a possible segmentation fault during concurrent executions of + pthread_exit(), a dummy thread is spawned which initializes internal variables + of pthread lib. See bug description for thoroughfull explanation. + + TODO: Remove this code when fixed versions of glibc6 are in common use. +*/ + + pthread_t dummy_thread; + pthread_attr_t dummy_thread_attr; + + pthread_attr_init(&dummy_thread_attr); + pthread_attr_setdetachstate(&dummy_thread_attr,PTHREAD_CREATE_DETACHED); + + pthread_create(&dummy_thread,&dummy_thread_attr,nptl_pthread_exit_hack_handler,NULL); + +#endif #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP /* From c82cd4a46eea3e44f948bf48c9175ef6883d5f95 Mon Sep 17 00:00:00 2001 From: "rafal@quant.(none)" <> Date: Fri, 8 Dec 2006 19:23:12 +0100 Subject: [PATCH 2/2] Minor fix --- include/my_pthread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/my_pthread.h b/include/my_pthread.h index 8051d75c59e..24f3fd62b8e 100644 --- a/include/my_pthread.h +++ b/include/my_pthread.h @@ -45,7 +45,7 @@ extern "C" { TODO: Remove this code when fixed versions of glibc6 are in common use. */ -#if defined(TARGET_OS_LINUX) && defined(HAVE_NPTL) && +#if defined(TARGET_OS_LINUX) && defined(HAVE_NPTL) && \ defined(__GLIBC__) && ( __GLIBC__ < 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ < 5 ) #define NPTL_PTHREAD_EXIT_BUG 1 #endif