Subject: segfault on simultaneous initialization

From: Sheheryar Parvaz <[email-masked]>
Date: Fri, 26 Jan 2018 17:24:11 +0000

If the initial call to scm_with_guile is on a thread and in the main
thread at the same time, a segmentation fault occurs.

    #include <pthread.h>
    #include <libguile.h>

    void* hello_world(void *arg) {
        scm_c_eval_string("(display \"Hello World!\")");
        scm_c_eval_string("(newline)");
    }

    void run_guile() {
        scm_with_guile(&hello_world, NULL);
    }

    int main(int argc, char **argv) {

        pthread_t th;
        int ret = pthread_create(&th, NULL, &run_guile, NULL);

        run_guile();
        pthread_join(&th, NULL);

        return 0;
    }

Here is a backtrace of the issue.

    #0  0x00007ffff7afd976 in scm_set_current_dynamic_state () from /usr/lib/libguile-2.2.so.1
    #1  0x00007ffff7b5faec in guilify_self_2 () from /usr/lib/libguile-2.2.so.1
    #2  0x00007ffff7b604b4 in scm_i_init_thread_for_guile () from /usr/lib/libguile-2.2.so.1
    #3  0x00007ffff7b604f9 in with_guile () from /usr/lib/libguile-2.2.so.1
    #4  0x00007ffff7281312 in GC_call_with_stack_base () from /usr/lib/libgc.so.1
    #5  0x00007ffff7b60918 in scm_with_guile () from /usr/lib/libguile-2.2.so.1
    #6  0x00000000004007a3 in run_guile ()
    #7  0x00007ffff7893568 in start_thread (arg=0x7ffff5ea8700) at pthread_create.c:465
    #8  0x00007ffff75cb52f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95