[Gc] Calling ‘GC_pthread_create’ from a pthread key destructor

Ludovic Courtès ludo at gnu.org
Wed Apr 20 07:20:08 PDT 2011


Hello,

The program below quickly segfaults on GNU/Linux:

--8<---------------cut here---------------start------------->8---
#define GC_THREADS 1
#define GC_NO_THREAD_REDIRECTS 1

#include <gc/gc.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>

static pthread_key_t key;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;

static void *
entry (void *arg)
{
  pthread_setspecific (key, GC_STRDUP ("hello, world"));
  return arg;
}

static void
on_thread_exit (void *v)
{
  pthread_t t;
  GC_pthread_create (&t, NULL, entry, NULL);
}

static void
make_key ()
{
  pthread_key_create (&key, on_thread_exit);
}

int
main (int argc, char *argv[])
{
  GC_INIT ();

  pthread_once (&key_once, make_key);

  while (1)
    {
      pthread_t t;
      GC_pthread_create (&t, NULL, entry, NULL);
    }

  return EXIT_SUCCESS;
}
--8<---------------cut here---------------end--------------->8---

The backtrace varies, but it’s typically one of:

--8<---------------cut here---------------start------------->8---
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fff7d722700 (LWP 1497)]
GC_generic_malloc_inner (lb=56, k=1) at ../malloc.c:127
127             obj_link(op) = 0;
(gdb) bt
#0  GC_generic_malloc_inner (lb=56, k=1) at ../malloc.c:127
#1  0x00007ffff7b916ef in GC_pthread_create (new_thread=0x7fff7d721e98, attr=0x0, start_routine=0x400904 <entry>, arg=0x0) at ../pthread_support.c:1474
#2  0x000000000040095a in on_thread_exit (v=0x662d90) at t.c:24
#3  0x00007ffff75f0b29 in __nptl_deallocate_tsd () from /nix/store/vxycd107wjbhcj720hzkw2px7s7kr724-glibc-2.12.2/lib/libpthread.so.0
#4  0x00007ffff75f0cfa in start_thread () from /nix/store/vxycd107wjbhcj720hzkw2px7s7kr724-glibc-2.12.2/lib/libpthread.so.0
#5  0x00007ffff78d81ed in clone () from /nix/store/vxycd107wjbhcj720hzkw2px7s7kr724-glibc-2.12.2/lib/libc.so.6
--8<---------------cut here---------------end--------------->8---

or:

--8<---------------cut here---------------start------------->8---
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fff9274c700 (LWP 1823)]
0x0000000000652fc0 in ?? ()
(gdb) bt
#0  0x0000000000652fc0 in ?? ()
#1  0x00007ffff7b90791 in GC_inner_start_routine (sb=<value optimized out>, arg=<value optimized out>) at ../pthread_start.c:61
#2  0x00007ffff7b8b6b5 in GC_call_with_stack_base (fn=<value optimized out>, arg=<value optimized out>) at ../misc.c:1505
#3  0x00007ffff75f0cec in start_thread () from /nix/store/vxycd107wjbhcj720hzkw2px7s7kr724-glibc-2.12.2/lib/libpthread.so.0
#4  0x00007ffff78d81ed in clone () from /nix/store/vxycd107wjbhcj720hzkw2px7s7kr724-glibc-2.12.2/lib/libc.so.6
--8<---------------cut here---------------end--------------->8---

Replacing ‘GC_pthread_create’ by ‘pthread_create’ in ‘on_thread_exit’
suffices to avoid the segfault.

Any ideas?  Is it something we can reasonably expect to work?

Thanks,
Ludo’.

PS: For the record, Guile has a similar problem with ‘scm_spawn_thread’.



More information about the Gc mailing list