From 6bfae0cd735052b91151f8ffc56808d090683483 Mon Sep 17 00:00:00 2001 From: brain Date: Thu, 20 Jul 2006 18:02:09 +0000 Subject: Fixed, no longer a memory leak (Thread leak) git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4465 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/cmd_nick.cpp | 5 ++++- src/dns.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++-------------- src/dnsqueue.cpp | 15 ++++++++++++- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/cmd_nick.cpp b/src/cmd_nick.cpp index 4db2a53bb..17dfa626e 100644 --- a/src/cmd_nick.cpp +++ b/src/cmd_nick.cpp @@ -151,7 +151,10 @@ void cmd_nick::Handle (const char** parameters, int pcnt, userrec *user) { #ifdef THREADED_DNS // initialize their dns lookup thread - if (pthread_create(&user->dnsthread, NULL, dns_task, (void *)user) != 0) + pthread_attr_t attribs; + pthread_attr_init(&attribs); + pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED); + if (pthread_create(&user->dnsthread, &attribs, dns_task, (void *)user) != 0) { log(DEBUG,"Failed to create DNS lookup thread for user %s: %s",user->nick, strerror(errno)); } diff --git a/src/dns.cpp b/src/dns.cpp index a8ed78cb6..f8433683d 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -475,6 +475,9 @@ char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS if (n_iter == connections.end()) { log(DEBUG,"DNS: got a response for a query we didnt send with fd=%d",cfd); +#ifdef THREADED_DNS + pthread_mutex_unlock(&connmap_lock); +#endif return NULL; } else @@ -879,34 +882,63 @@ void* dns_task(void* arg) int thisfd = u->fd; log(DEBUG,"DNS thread for user %s",u->nick); - DNS dns1; - DNS dns2; + DNS dns1(Config->DNSServer); + DNS dns2(Config->DNSServer); std::string host; std::string ip; + int iterations = 0; + + log(DEBUG,"Thread loc 1"); if (dns1.ReverseLookup(inet_ntoa(u->ip4),false)) { - while (!dns1.HasResult()) + log(DEBUG,"Thread loc 2"); + /* FIX: Dont make these infinite! */ + while ((!dns1.HasResult()) && (++iterations < 20)) usleep(100); - host = dns1.GetResult(); - if (host != "") + + log(DEBUG,"Thread loc 3"); + if (iterations < 20) { - if (dns2.ForwardLookup(host, false)) + log(DEBUG,"Thread loc 4"); + if (dns1.GetFD() != -1) { - while (!dns2.HasResult()) - usleep(100); - ip = dns2.GetResultIP(); - if (ip == std::string(inet_ntoa(u->ip4))) + host = dns1.GetResult(); + if (host != "") { - if (host.length() < 65) + log(DEBUG,"Thread loc 5"); + if (dns2.ForwardLookup(host, false)) { - if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd])) + iterations = 0; + while ((!dns2.HasResult()) && (++iterations < 20)) + usleep(100); + + log(DEBUG,"Thread loc 6"); + if (iterations < 20) { - if (!u->dns_done) + if (dns2.GetFD() != -1) { - strcpy(u->host,host.c_str()); - if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd])) + log(DEBUG,"Thread loc 7"); + ip = dns2.GetResultIP(); + if (ip == std::string(inet_ntoa(u->ip4))) { - strcpy(u->dhost,host.c_str()); + log(DEBUG,"Thread loc 8"); + if (host.length() < 65) + { + log(DEBUG,"Thread loc 9"); + if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd])) + { + log(DEBUG,"Thread loc 10"); + if (!u->dns_done) + { + log(DEBUG,"Thread loc 11"); + strcpy(u->host,host.c_str()); + if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd])) + { + strcpy(u->dhost,host.c_str()); + } + } + } + } } } } @@ -915,9 +947,11 @@ void* dns_task(void* arg) } } } + log(DEBUG,"Thread loc 12"); if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd])) u->dns_done = true; - pthread_exit(0); + log(DEBUG,"THREAD EXIT"); + return NULL; } #endif diff --git a/src/dnsqueue.cpp b/src/dnsqueue.cpp index 35dc1bba1..fb0e0bd75 100644 --- a/src/dnsqueue.cpp +++ b/src/dnsqueue.cpp @@ -39,6 +39,7 @@ class Lookup; Lookup* dnslist[MAX_DESCRIPTORS]; Lookup* user_fd_to_dns[MAX_DESCRIPTORS]; +extern userrec* fd_ref_table[MAX_DESCRIPTORS]; //enum LookupState { reverse, forward }; @@ -239,7 +240,19 @@ bool lookup_dns(const std::string &nick) void ZapThisDns(int fd) { -#ifndef THREADED_DNS +#ifdef THREADED_DNS +/* if (fd_ref_table[fd]) + { + if (fd_ref_table[fd]->registered >= 3) + { + log(DEBUG,"Joining thread for user %d",fd); + if (pthread_join(fd_ref_table[fd]->dnsthread, NULL)) + { + log(DEBUG,"Can't pthread_join(): %s", strerror(errno)); + } + } + }*/ +#else if ((fd < 0) || (fd > MAX_DESCRIPTORS)) return; -- cgit v1.2.3