diff options
author | Ivan Maidanski <ivmai@mail.ru> | 2023-04-04 09:21:28 +0300 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2023-04-06 13:16:52 +0300 |
commit | 68d419a51fa73765ac66beb348a6c577ca1963ac (patch) | |
tree | 67402be765bbb6006354696b70ad4a6c7d21b708 /pthread_support.c | |
parent | 5f16c0ab09760910e8995317ebc51b4fae198101 (diff) | |
download | bdwgc-68d419a51fa73765ac66beb348a6c577ca1963ac.tar.gz |
Fix signals delivery fail in find-leak mode if init from non-main thread
Issue #542 (bdwgc).
The scenario of the failure (on Linux):
- Find-leak mode is on
- pthread_create/join primitives are not redirected
- GC is initialized from a thread other than primordial
- that thread is terminated (but not unregistered as the 1st one in GC)
- the primordial thread exits and launches GC_exit_check
- GC_gcollect tries to suspend the terminated thread by sending a signal
- the signal is not delivered but pthread_kill just returns zero
- the signal sending is retried until aborting with the proper message.
The proposed solution (workaround) is not to call GC_gcollect at
the process exit (in the find-leak mode) if GC_init was called from
a thread other than primordial one.
Check in GC_exit_check() (which is called if GC_find_leak) that the
thread executing at-exit functions is the same as the one performed
the GC initialization, otherwise the latter thread might already be
dead but still registered and this, as a consequence, might cause
a signal delivery fail when suspending the threads on platforms that
do not guarantee ESRCH returned if the signal is not delivered (as
observed on Ubuntu 22).
* include/private/gc_priv.h [THREADS && !DONT_USE_ATEXIT]
(GC_is_main_thread): Declare GC_INNER function.
* include/private/pthread_support.h [GC_WIN32_THREADS
&& !DONT_USE_ATEXIT] (GC_main_thread_id): Declare variable.
* include/private/pthread_support.h [GC_WIN32_THREADS
&& !GC_NO_THREADS_DISCOVERY] (GC_main_thread_id): Do not depend on
GC_ASSERTIONS.
* misc.c [!DONT_USE_ATEXIT && THREADS] (GC_exit_check): If not
GC_is_main_thread() or not GC_thread_is_registered() then do not call
GC_gcollect(); add comment; do not set and clear GC_in_thread_creation.
* pthread_support.c [!GC_NO_THREADS_DISCOVERY && GC_WIN32_THREADS
|| !DONT_USE_ATEXIT] (GC_main_thread_id): Define variable.
* pthread_support.c [!DONT_USE_ATEXIT] (GC_is_main_thread): Implement.
* pthread_support.c [!GC_WIN32_THREADS] (GC_thr_init): Define self_id
local variable.
* pthread_support.c [!GC_WIN32_THREADS && !DONT_USE_ATEXIT]
(GC_thr_init): Set GC_main_thread_id.
* win32_threads.c [!DONT_USE_ATEXIT] (GC_thr_init): Likewise.
* win32_threads.c [!GC_NO_THREADS_DISCOVERY] (GC_main_thread_id): Do
not define variable.
Diffstat (limited to 'pthread_support.c')
-rw-r--r-- | pthread_support.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/pthread_support.c b/pthread_support.c index 7e42b1bc..30addcbe 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -1489,6 +1489,19 @@ GC_INNER_WIN32THREAD void GC_record_stack_base(GC_stack_context_t crtn, # endif } +#if !defined(GC_NO_THREADS_DISCOVERY) && defined(GC_WIN32_THREADS) \ + || !defined(DONT_USE_ATEXIT) + GC_INNER_WIN32THREAD thread_id_t GC_main_thread_id; +#endif + +#ifndef DONT_USE_ATEXIT + GC_INNER GC_bool GC_is_main_thread(void) + { + GC_ASSERT(GC_thr_initialized); + return thread_id_self() == GC_main_thread_id; + } +#endif /* !DONT_USE_ATEXIT */ + #ifndef GC_WIN32_THREADS STATIC GC_thread GC_register_my_thread_inner(const struct GC_stack_base *sb, @@ -1645,6 +1658,7 @@ GC_INNER void GC_thr_init(void) struct GC_stack_base sb; GC_thread me; GC_stack_context_t crtn; + thread_id_t self_id = thread_id_self(); sb.mem_base = GC_stackbottom; GC_ASSERT(sb.mem_base != NULL); @@ -1654,7 +1668,10 @@ GC_INNER void GC_thr_init(void) sb.reg_base = NULL; # endif GC_ASSERT(NULL == GC_self_thread_inner()); - me = GC_register_my_thread_inner(&sb, thread_id_self()); + me = GC_register_my_thread_inner(&sb, self_id); +# ifndef DONT_USE_ATEXIT + GC_main_thread_id = self_id; +# endif me -> flags = DETACHED; /* Copy the alt-stack information if set. */ crtn = me -> crtn; |