From 498d6eb114c52bd16a6821029959e88ed8f87396 Mon Sep 17 00:00:00 2001 From: Misaki Shioi <31817032+shioimm@users.noreply.github.com> Date: Wed, 18 Dec 2024 09:48:26 +0900 Subject: [PATCH] Wrap `do_fast_fallback_getaddrinfo` with `rb_thread_prevent_fork` (#12366) Wrap `do_fast_fallback_getaddrinfo` with `rb_thread_prevent_fork` Referencing PR #10864, wrap `do_fast_fallback_getaddrinfo` with `rb_thread_prevent_fork` to avoid fork safety issues. `do_fast_fallback_getaddrinfo` internally uses getaddrinfo(3), leading to fork safety issues, as described in PR #10864. This change ensures that `do_fast_fallback_getaddrinfo` is guarded by `rb_thread_prevent_fork`, preventing fork during its execution and avoiding related issues. --- ext/socket/ipsocket.c | 2 +- ext/socket/raddrinfo.c | 8 +++++++- ext/socket/rubysocket.h | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c index f991e2a742..05f71343e3 100644 --- a/ext/socket/ipsocket.c +++ b/ext/socket/ipsocket.c @@ -637,7 +637,7 @@ init_fast_fallback_inetsock_internal(VALUE v) } } - if (raddrinfo_pthread_create(&threads[i], do_fast_fallback_getaddrinfo, arg->getaddrinfo_entries[i]) != 0) { + if (raddrinfo_pthread_create(&threads[i], fork_safe_do_fast_fallback_getaddrinfo, arg->getaddrinfo_entries[i]) != 0) { rsock_raise_resolution_error("getaddrinfo(3)", EAI_AGAIN); } pthread_detach(threads[i]); diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index dc36c3aa31..bd39b35390 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -3048,7 +3048,7 @@ free_fast_fallback_getaddrinfo_entry(struct fast_fallback_getaddrinfo_entry **en *entry = NULL; } -void * +static void * do_fast_fallback_getaddrinfo(void *ptr) { struct fast_fallback_getaddrinfo_entry *entry = (struct fast_fallback_getaddrinfo_entry *)ptr; @@ -3117,6 +3117,12 @@ do_fast_fallback_getaddrinfo(void *ptr) return 0; } +void * +fork_safe_do_fast_fallback_getaddrinfo(void *ptr) +{ + return rb_thread_prevent_fork(do_fast_fallback_getaddrinfo, ptr); +} + #endif /* diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h index 932ed8e022..92e0f2604d 100644 --- a/ext/socket/rubysocket.h +++ b/ext/socket/rubysocket.h @@ -447,7 +447,7 @@ struct fast_fallback_getaddrinfo_shared }; int raddrinfo_pthread_create(pthread_t *th, void *(*start_routine) (void *), void *arg); -void *do_fast_fallback_getaddrinfo(void *ptr); +void *fork_safe_do_fast_fallback_getaddrinfo(void *ptr); void free_fast_fallback_getaddrinfo_entry(struct fast_fallback_getaddrinfo_entry **entry); void free_fast_fallback_getaddrinfo_shared(struct fast_fallback_getaddrinfo_shared **shared); # endif