From d0066211f2052bf1444ffeb11544860a12cebff2 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 6 Nov 2023 20:30:56 +0900 Subject: [PATCH] Detach a pthread after pthread_setaffinity_np After a pthread for getaddrinfo is detached, we cannot predict when the thread will exit. It would lead to a segfault by setting pthread_setaffinity to the terminated pthread. I guess this problem would be more likely to occur in high-load environments. This change detaches the pthread after pthread_setaffinity is called. [Feature #19965] --- ext/socket/raddrinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index 3a60330cce..f5c8ca592a 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -481,13 +481,13 @@ start: return EAI_AGAIN; } - pthread_detach(th); #if defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU) cpu_set_t tmp_cpu_set; CPU_ZERO(&tmp_cpu_set); CPU_SET(sched_getcpu(), &tmp_cpu_set); pthread_setaffinity_np(th, sizeof(cpu_set_t), &tmp_cpu_set); #endif + pthread_detach(th); rb_thread_call_without_gvl2(wait_getaddrinfo, arg, cancel_getaddrinfo, arg); @@ -700,13 +700,13 @@ start: return EAI_AGAIN; } - pthread_detach(th); #if defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU) cpu_set_t tmp_cpu_set; CPU_ZERO(&tmp_cpu_set); CPU_SET(sched_getcpu(), &tmp_cpu_set); pthread_setaffinity_np(th, sizeof(cpu_set_t), &tmp_cpu_set); #endif + pthread_detach(th); rb_thread_call_without_gvl2(wait_getnameinfo, arg, cancel_getnameinfo, arg);