0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-10 08:07:34 +00:00

fix random crashes on pthread_detach() ()

This commit is contained in:
Costa Tsaousis 2023-10-06 10:12:07 +03:00 committed by GitHub
parent f97eef550e
commit 3581d73519
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,8 +9,8 @@ static pthread_attr_t *netdata_threads_attr = NULL;
typedef struct {
void *arg;
pthread_t *thread;
char tag[NETDATA_THREAD_NAME_MAX + 1];
SPINLOCK detach_lock;
void *(*start_routine) (void *);
NETDATA_THREAD_OPTIONS options;
} NETDATA_THREAD;
@ -185,6 +185,7 @@ static void thread_cleanup(void *ptr) {
NETDATA_THREAD *info = (NETDATA_THREAD *)ptr;
netdata_log_error("THREADS: internal error - thread local variable does not match the one passed to this function. Expected thread '%s', passed thread '%s'", netdata_thread->tag, info->tag);
}
spinlock_lock(&netdata_thread->detach_lock);
if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
netdata_log_info("thread with task id %d finished", gettid());
@ -199,6 +200,7 @@ static void thread_cleanup(void *ptr) {
netdata_thread->tag[0] = '\0';
spinlock_unlock(&netdata_thread->detach_lock);
freez(netdata_thread);
netdata_thread = NULL;
}
@ -281,13 +283,15 @@ static void *netdata_thread_init(void *ptr) {
}
int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg) {
NETDATA_THREAD *info = mallocz(sizeof(NETDATA_THREAD));
NETDATA_THREAD *info = callocz(1, sizeof(NETDATA_THREAD));
info->arg = arg;
info->thread = thread;
info->start_routine = start_routine;
info->options = options;
strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX);
spinlock_init(&info->detach_lock);
spinlock_lock(&info->detach_lock);
int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info);
if(ret != 0)
netdata_log_error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
@ -300,6 +304,7 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR
}
}
spinlock_unlock(&info->detach_lock);
return ret;
}