From 1858233ffaee482a73a91b796f02ebb7ae1306b9 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 3 Oct 2025 19:51:01 -0400 Subject: [PATCH] Free the native thread of the main thread on FREE_AT_EXIT --- internal/thread.h | 2 ++ thread.c | 9 +++++++++ vm.c | 3 +-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/thread.h b/internal/thread.h index 928126c3b0..21efeeebc0 100644 --- a/internal/thread.h +++ b/internal/thread.h @@ -64,6 +64,8 @@ void rb_thread_io_close_wait(struct rb_io *); void rb_ec_check_ints(struct rb_execution_context_struct *ec); +void rb_thread_free_native_thread(void *th_ptr); + RUBY_SYMBOL_EXPORT_BEGIN void *rb_thread_prevent_fork(void *(*func)(void *), void *data); /* for ext/socket/raddrinfo.c */ diff --git a/thread.c b/thread.c index 5556100102..0419fdf054 100644 --- a/thread.c +++ b/thread.c @@ -529,6 +529,15 @@ thread_cleanup_func(void *th_ptr, int atfork) rb_native_mutex_destroy(&th->interrupt_lock); } +void +rb_thread_free_native_thread(void *th_ptr) +{ + rb_thread_t *th = th_ptr; + + native_thread_destroy_atfork(th->nt); + th->nt = NULL; +} + static VALUE rb_threadptr_raise(rb_thread_t *, int, VALUE *); static VALUE rb_thread_to_s(VALUE thread); diff --git a/vm.c b/vm.c index 8bdf9e4fc2..c9bf3dce36 100644 --- a/vm.c +++ b/vm.c @@ -3306,8 +3306,7 @@ ruby_vm_destruct(rb_vm_t *vm) rb_id_table_free(vm->constant_cache); set_free_table(vm->unused_block_warning_table); - xfree(th->nt); - th->nt = NULL; + rb_thread_free_native_thread(th); #ifndef HAVE_SETPROCTITLE ruby_free_proctitle();