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]
This commit is contained in:
Yusuke Endoh 2023-11-06 20:30:56 +09:00
Родитель 15560cce5f
Коммит d0066211f2
1 изменённых файлов: 2 добавлений и 2 удалений

Просмотреть файл

@ -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);