From e615da39b8080b25bb95e5f6ff4d035911c2467d Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Thu, 14 Nov 2024 09:09:10 -0600 Subject: [PATCH] Fix SystemExit segfault for non-main threads I'm not going to pretend I understand the undefined behavior here, but I *think* running detach_all while we ourselves are in one of those threads is causing something to go haywire. Only running this on the main thread seems to fix a segfault when you run code like this: Thread.new { Process.exit! 21 }.join sleep --- src/natalie.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/natalie.cpp b/src/natalie.cpp index 9ae759a24..ac14ff3e5 100644 --- a/src/natalie.cpp +++ b/src/natalie.cpp @@ -837,10 +837,12 @@ Value super(Env *env, Value self, Args args, Block *block) { } void clean_up_and_exit(int status) { - ThreadObject::detach_all(); - if (Heap::the().collect_all_at_exit()) { - delete &Heap::the(); - delete NativeProfiler::the(); + if (ThreadObject::i_am_main()) { + ThreadObject::detach_all(); + if (Heap::the().collect_all_at_exit()) { + delete &Heap::the(); + delete NativeProfiler::the(); + } } exit(status); }