Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash fix: dont create threads as detached but join #1460

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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]);
Expand All @@ -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 */
Expand All @@ -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) {
Expand All @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions src/eggdrop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down