Impossible to reinitialize codegen subsystem in julia #59585
Replies: 8 comments 1 reply
-
You are not supposed to tear down the internal state if you need to use it again, since it will introduce thread safety violations. Do not do that, and the problem should resolve itself. |
Beta Was this translation helpful? Give feedback.
-
@vtjnash I work in such pattern: static void *task_thread(void *arg) {
UNUSED(arg);
jl_adopt_thread();
// Heavy julia logic jl_load(), jl_call() etc.
}
int main() {
pthread_t tid;
julia_init();
pthread_create(&tid, task_thread, NULL);
// Wait stop command from another process
pthread_cancel(tid);
} If stop command received when the thread evaluate julia code (especially runtime codegen and compilation), julia context of process remains absolutely invalid. The next static void *task_thread(void *arg) {
UNUSED(arg);
jl_adopt_thread();
// Heavy julia logic jl_load(), jl_call() etc.
}
int main() {
pthread_t tid;
julia_init();
pthread_create(&tid, task_thread, NULL);
// Wait stop command from another process
pthread_cancel(tid);
pthread_create(&tid, task_thread, NULL); // Task starts but freezes on the first jl_call()
} To overcome it I found a way based on thread destructors static pthread_key_t task_key;
static void *task_thread(void *arg) {
pthread_setspecific(task_key, arg);
jl_adopt_thread();
// Heavy julia logic jl_load(), jl_call() etc.
}
static void task_destructor(void *arg) {
jl_teardown_codegen(); // It completes fine
jl_init_codegen(); // It aborts without my patch in jitlayers.cpp
JL_MUTEX_INIT(&jl_codegen_lock, "jl_codegen_lock");
}
int main() {
pthread_t tid;
void *thread_data = NULL; // Really it is something
julia_init();
pthread_key_create(&task_key, task_destructor);
pthread_create(&tid, task_thread, thread_data);
// Wait stop command from another process
pthread_cancel(tid);
pthread_create(&tid, task_thread, NULL); // With patch it works fine
} After codegen reinitialization julia remains workable, but is is only possible with pull request, attached to this issue. P.S. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
There is a similar function exported by julia ( |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@vtjnash @fingolfin ptls->io_wait = 1;
jl_atomic_store_release(&ptls->signal_request, 2);
pthread_kill(ptls->system_id, SIGUSR2); instead of Lines 633 to 646 in e0d49ac What do you think about such solution? |
Beta Was this translation helpful? Give feedback.
-
If that segfaults, you might have forgotten to register signal handlers (jl_options.handle_signals = 1)? If so, that is another time-bomb in your code waiting to happen, since it is infrequent but will cause segfaults eventually. |
Beta Was this translation helpful? Give feedback.
-
Converted to a discussion for more formatting controls, but could also move to the Discourse forum. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I work with embedded
julia
(I don't use julia interpreter, I uselibjulia-*
libraries directly) and due to certain problems (forced termination of the adopted thread (it makes with help ofjl_adopt_thread()
API, which executes julia code)julia/src/threading.c
Line 436 in 7de5585
It became necessary to reinitialize the code generation subsystem (calling
jl_teardown_codegen()
andjl_init_codegen()
), since julia remained in an incorrect state when the thread was terminated (locks in the code generation subsystem were not released). However, the reinitialization fails due to the "Library already loaded" error in the sys::DynamicLibrary::addPermanentLibrary (julia/src/jitlayers.cpp
Lines 1945 to 1948 in 7de5585
libjulia-internal
(obviously, because it has already been loaded before) and as a result, abortion is called. Ignoring this error completely solves this problem. The documentation for theaddPermanentLibrary
function says that it can be safely called many times for the same library.My suggestion is to ignore this error on initialization step:
Beta Was this translation helpful? Give feedback.
All reactions