From 18632bd1fd8048af6c6726bd743896e561cf74dc Mon Sep 17 00:00:00 2001 From: kein name Date: Mon, 24 Jul 2023 17:21:47 +0200 Subject: [PATCH 1/2] dont create threads as detached but join --- src/dns.c | 18 ++---------------- src/eggdrop.h | 1 + src/net.c | 3 +++ 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/dns.c b/src/dns.c index afa407b07..66d14b847 100644 --- a/src/dns.c +++ b/src/dns.c @@ -568,7 +568,6 @@ void *thread_dns_ipbyhost(void *arg) void core_dns_hostbyip(sockname_t *addr) { struct dns_thread_node *dtn = nmalloc(sizeof(struct dns_thread_node)); - pthread_t thread; /* only used by pthread_create(), no need to save */ pthread_attr_t attr; if (pthread_attr_init(&attr)) { @@ -577,12 +576,6 @@ void core_dns_hostbyip(sockname_t *addr) nfree(dtn); return; } - if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { - putlog(LOG_MISC, "*", "core_dns_hostbyip(): pthread_attr_setdetachstate(): error = %s", strerror(errno)); - call_hostbyip(addr, iptostr(&addr->addr.sa), 0); - nfree(dtn); - return; - } if (pthread_mutex_init(&dtn->mutex, NULL)) fatal("ERROR: core_dns_hostbyip(): pthread_mutex_init() failed", 0); if (pipe(dtn->fildes) < 0) { @@ -592,7 +585,7 @@ void core_dns_hostbyip(sockname_t *addr) return; } memcpy(&dtn->addr, addr, sizeof *addr); - if (pthread_create(&thread, &attr, thread_dns_hostbyip, (void *) dtn)) { + if (pthread_create(&(dtn->thread_id), &attr, thread_dns_hostbyip, (void *) dtn)) { putlog(LOG_MISC, "*", "core_dns_hostbyip(): pthread_create(): error = %s", strerror(errno)); call_hostbyip(addr, iptostr(&addr->addr.sa), 0); close(dtn->fildes[0]); @@ -609,7 +602,6 @@ void core_dns_ipbyhost(char *host) { sockname_t addr; struct dns_thread_node *dtn; - pthread_t thread; /* only used by pthread_create(), no need to save */ pthread_attr_t attr; /* if addr is ip instead of host */ @@ -624,12 +616,6 @@ void core_dns_ipbyhost(char *host) nfree(dtn); return; } - if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { - putlog(LOG_MISC, "*", "core_dns_ipbyhost(): pthread_attr_setdetachstate(): error = %s", strerror(errno)); - call_ipbyhost(host, &addr, 0); - nfree(dtn); - return; - } if (pthread_mutex_init(&dtn->mutex, NULL)) fatal("ERROR: core_dns_ipbyhost(): pthread_mutex_init() failed", 0); if (pipe(dtn->fildes) < 0) { @@ -641,7 +627,7 @@ void core_dns_ipbyhost(char *host) dtn->next = dns_thread_head->next; dns_thread_head->next = dtn; strlcpy(dtn->host, host, sizeof dtn->host); - if (pthread_create(&thread, &attr, thread_dns_ipbyhost, (void *) dtn)) { + if (pthread_create(&(dtn->thread_id), &attr, thread_dns_ipbyhost, (void *) dtn)) { putlog(LOG_MISC, "*", "core_dns_ipbyhost(): pthread_create(): error = %s", strerror(errno)); call_ipbyhost(host, &addr, 0); close(dtn->fildes[0]); diff --git a/src/eggdrop.h b/src/eggdrop.h index 0b7015648..900569ba7 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -793,6 +793,7 @@ enum { /* linked list instead of array because of multi threading */ struct dns_thread_node { + pthread_t thread_id; pthread_mutex_t mutex; int fildes[2]; int type; diff --git a/src/net.c b/src/net.c index 48a5c9e60..f1413e21b 100644 --- a/src/net.c +++ b/src/net.c @@ -898,6 +898,7 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) #ifdef EGG_TDNS int fd; struct dns_thread_node *dtn, *dtn_prev; + void *res; #endif maxfd_r = preparefdset(&fdr, slist, slistmax, tclonly, TCL_READABLE); @@ -1065,6 +1066,8 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) pthread_mutex_unlock(&dtn->mutex); } close(dtn->fildes[0]); + if (!pthread_join(dtn->thread_id, &res)) + putlog(LOG_MISC, "*", "sockread(): pthread_join(): error = %s", strerror(errno)); dtn_prev->next = dtn->next; nfree(dtn); dtn = dtn_prev; From 42eb9a61745fcab48f7aeb50e6e91a1af9bfaa08 Mon Sep 17 00:00:00 2001 From: kein name Date: Mon, 24 Jul 2023 19:49:05 +0200 Subject: [PATCH 2/2] Fix error handling --- src/net.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net.c b/src/net.c index f1413e21b..6d5c8657a 100644 --- a/src/net.c +++ b/src/net.c @@ -1066,7 +1066,7 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) pthread_mutex_unlock(&dtn->mutex); } close(dtn->fildes[0]); - if (!pthread_join(dtn->thread_id, &res)) + if (pthread_join(dtn->thread_id, &res)) putlog(LOG_MISC, "*", "sockread(): pthread_join(): error = %s", strerror(errno)); dtn_prev->next = dtn->next; nfree(dtn);