Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault when calling back from a thread, created in C environment #43659

Closed
KlausC opened this issue Jan 4, 2022 · 1 comment
Closed

Segfault when calling back from a thread, created in C environment #43659

KlausC opened this issue Jan 4, 2022 · 1 comment

Comments

@KlausC
Copy link
Contributor

KlausC commented Jan 4, 2022

I want to use the main processing loop of a C-library, which implements specific functionality in callbacks.
The callbacks shall be implemented in Julia. The following MWE demonstrates the difficulties, when the callback functions are invoked from POSIX-threads, which have been launched in C.

callmtcb.jl

module Testcb
    f1() = println("called f1()")
    f2() = exit(99)
    cf1 = @cfunction f1 Cvoid ()
    cf2 = @cfunction f2 Cvoid ()

    push!(Base.DL_LOAD_PATH, pwd())
    dl = Base.Libc.dlopen("libmtcb")
    dls = Base.Libc.Libdl.dlsym(dl, :cb_setup)
    ccall(dls, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), cf1, cf2)
end

libmtcb.c

#include <pthread.h>

pthread_t threadid;
void(*savedf2)(void);

void* cb_thread(void* arg);

void cb_setup(void(*f1)(void), void(*f2)(void)) {

    (*f1)();
    savedf2 = f2;

    pthread_create(&threadid, NULL, cb_thread, (void*)NULL);

    pthread_join(threadid, NULL);
    pthread_exit((void*)0);
}

void* cb_thread(void* arg) {
    (*savedf2)();
    return (void*)0;
}

Result of execution:

 make run
/home/crusius/julia/julia --startup-file=no callmtcb.jl
called f1()

signal (11): Segmentation fault
in expression starting at /home/crusius/dev/FuseApi/test/embed/callmtcb.jl:11
Allocations: 2749 (Pool: 2739; Big: 10); GC: 0
make: *** [Makefile:18: run] Segmentation fault (core dumped)

A gdb session revealed, that the callback to f2 propagates into the call to exit(99) and then fails.
Actually the threads local variable jl_pgcstack is not initialized, so the Julia runtime has no chance to succeed.

The test files and the setup for a debug session are found here: https://github.com/KlausC/FuseApi.jl/blob/main/test/embed/
There is a Makefile with run and debug targets to explore the example under Linux and Gnome.

@vchuravy
Copy link
Member

vchuravy commented Jan 4, 2022

This is a duplicated of #17573. It is a known limitation that Julia functions can't be executed on a foreign thread.

You can attempt to do some minimal interactions an as an example call ccall(:uv_async_send, ...). To trigger an AsyncCondition on a Julia thread.

@vchuravy vchuravy closed this as completed Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants