summaryrefslogtreecommitdiffstats
path: root/libnetdata
diff options
context:
space:
mode:
authorCosta Tsaousis <costa@netdata.cloud>2023-10-06 10:12:07 +0300
committerGitHub <noreply@github.com>2023-10-06 10:12:07 +0300
commit3581d73519782b0800f994547e2cdb57c19c74be (patch)
tree106d21c4a8695b1055a1bc0d3c7c386a6b2be6f6 /libnetdata
parentf97eef550ed06521d52d87df97afea89d96f118d (diff)
fix random crashes on pthread_detach() (#16137)
Diffstat (limited to 'libnetdata')
-rw-r--r--libnetdata/threads/threads.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c
index 09e08cd188..cc5690600d 100644
--- a/libnetdata/threads/threads.c
+++ b/libnetdata/threads/threads.c
@@ -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;
}