Segmentation fault in runTlsDtors

Ali Çehreli acehreli at yahoo.com
Fri Jun 25 14:55:12 UTC 2021


I need your help with sporadic segfaults.

Players:

* dmd 2.096 (but I've seen similar issues in the past with earlier 
versions as well)

* A D library with extern(C) functions that calls rt_init() and 
rt_term(), which I think are needed for the library's use with Python

* A D program that uses said library (would calling rt_init() and 
rt_term() cause harm in this case?) (Using the library with Python works 
fine.)


The segfault happens when the program is shutting down. Here is a stack 
trace from a core dump:

[Current thread is 1 (Thread 0x7fb1ef95e700 (LWP 20010))]
(gdb) bt
#0 0x00007fb240a1c698 in 
_D2rt5minfo__T17runModuleFuncsRevSQBgQBg11ModuleGroup11runTlsDtorsMFZ9__lambda1ZQCoMFAxPyS6object10ModuleInfoZv 
() from /usr/lib/x86_64-linux-gnu/libphobos2.so.0.96
#1 0x00007fb240a1c0b1 in rt.minfo.ModuleGroup.runTlsDtors() () from 
/usr/lib/x86_64-linux-gnu/libphobos2.so.0.96
#2 0x00007fb240a1c411 in 
_D2rt5minfo16rt_moduleTlsDtorUZ14__foreachbody1MFKSQBx19sections_elf_shared3DSOZi 
() from /usr/lib/x86_64-linux-gnu/libphobos2.so.0.96
#3 0x00007fb240a1ddf2 in 
_D2rt19sections_elf_shared3DSO14opApplyReverseFMDFKSQByQByQBgZiZi () 
from /usr/lib/x86_64-linux-gnu/libphobos2.so.0.96
#4 0x00007fb240a1c3f1 in rt_moduleTlsDtor () from 
/usr/lib/x86_64-linux-gnu/libphobos2.so.0.96
#5 0x00007fb240a0a401 in thread_entryPoint () from 
/usr/lib/x86_64-linux-gnu/libphobos2.so.0.96
#6 0x00007fb23fcf56db in start_thread (arg=0x7fb1ef95e700) at 
pthread_create.c:463
#7 0x00007fb23f80671f in clone () at 
../sysdeps/unix/sysv/linux/x86_64/clone.S:95


If related, here are the library initialization and deinitialization 
functions, which I think are needed e.g. for using from Python:

// The initialization function of the library
pragma (crt_constructor)
extern (C)
void lib_init() {
   const err = rt_init();
   enum success = 1;  // Yes, backwards.
   if (err != success) {
     fprintf(core.stdc.stdio.stderr, "Failed to initialize D runtime.");
     abort();
   }
}

// The deinitialization function of the library
pragma (crt_destructor)
extern (C)
void lib_deinit() {
   const err = rt_term();
   enum success = 1;  // Yes, backwards.
   if (err != success) {
     fprintf(core.stdc.stdio.stderr, "Failed to deinitialize D runtime.");
     // Intentionally not aborting in a destructor.
   }
}


The segmentation fault is sporadic; likely due to a race condition. Is 
it related to my code? Can I workaround this? Can I reduce the 
likelihood of this happening?

The couple of places where I define any '~this' function is not used in 
this program. So, I rule out my allocating memory in a destructor.

Thank you,
Ali



More information about the Digitalmars-d mailing list