Subject: guile-2.2 threading bug
From: Linas Vepstas <[email-masked]>
To: [email-masked]
Date: Tue, 20 Dec 2016 14:13:44 -0600
List-Id: <debbugs-submit.debbugs.gnu.org>

Merry Christmas!  A guile-git threading bug!

-- linas

$ cat fail.cc
//
// fail.cc
//
// This C++ program crashes, when compiled against todays (20 Dec 2016)
// guile-2.2 from git. git log says a recent commit is
//    0ce8a9a5e01d3a12d83fea85968e1abb602c9298
// but I beielve any guile-2.2 version from late 2016 will crash.
//
// I built this as:
// cc fail.cc -I /usr/local/include/guile/2.2 -lguile-2.2  -lpthread -lstdc++
//
// gdb gives the following stack trace:
/*****
Thread 7 "a.out" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff3306700 (LWP 23578)]
0x00007ffff7b03076 in is_dynamic_state (x=0x0) at ../../libguile/fluids.c:97
97  return SCM_HAS_TYP7 (x, scm_tc7_dynamic_state);
(gdb) bt
#0  0x00007ffff7b03076 in is_dynamic_state (x=0x0) at ../../libguile/fluids.c:97
#1  scm_set_current_dynamic_state (state=state@entry=0x0)
    at ../../libguile/fluids.c:496
#2  0x00007ffff7b6351a in guilify_self_2 (dynamic_state=dynamic_state@entry=0x0)
    at ../../libguile/threads.c:466
#3  0x00007ffff7b63e0c in scm_i_init_thread_for_guile (base=0x7ffff3305df0,
    dynamic_state=0x0) at ../../libguile/threads.c:595
#4  0x00007ffff7b63e59 in with_guile (base=base@entry=0x7ffff3305df0,
    data=data@entry=0x7ffff3305e20) at ../../libguile/threads.c:638
#5  0x00007ffff6c15812 in GC_call_with_stack_base (
    fn=fn@entry=0x7ffff7b63e40 <with_guile>, arg=arg@entry=0x7ffff3305e20)
    at misc.c:1925
#6  0x00007ffff7b641f8 in scm_i_with_guile (dynamic_state=<optimized out>,
    data=<optimized out>, func=<optimized out>) at ../../libguile/threads.c:688
#7  scm_with_guile (func=<optimized out>, data=<optimized out>)
    at ../../libguile/threads.c:694
#8  0x0000555555555064 in foo(int) ()
#9  0x0000555555556776 in void std::_Bind_simple<void
(*(int))(int)>::_M_invoke<0ul>(std::_Index_tuple<0ul>) ()
#10 0x00005555555566c9 in std::_Bind_simple<void (*(int))(int)>::operator()() ()
#11 0x00005555555566a8 in
std::thread::_State_impl<std::_Bind_simple<void (*(int))(int)>
>::_M_run() ()
#12 0x00007ffff75cccdf in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#13 0x00007ffff789b464 in start_thread (arg=0x7ffff3306700) at
pthread_create.c:333
#14 0x00007ffff70469df in clone () at
../sysdeps/unix/sysv/linux/x86_64/clone.S:105
(gdb)
 ****/

#include <libguile.h>

#include <thread>
#include <vector>

void * wrap_foo(void *p)
{
   scm_c_eval_string ("(setlocale LC_ALL \"\")");
}

static volatile bool hold = true;

void foo(int thread_id)
{
   while (hold) {} // spin
   // A long sleep here avoids the crash
   // usleep(thread_id * 100000);
   scm_with_guile(wrap_foo, nullptr);
}

main()
{
   int n_threads = 12;
   std::vector<std::thread> thread_pool;
   for (int i=0; i < n_threads; i++)
      thread_pool.push_back(std::thread(&foo, i));

   printf("Done creating %d threads\n", n_threads);
   hold = false;

   for (std::thread& t : thread_pool) t.join();
   printf("Done joining %d threads\n", n_threads);
}