@cryptotaxi247 / netdata-1 / commits / d568fed16

memory leak fixed - if the was a client with gzip enabled and keep-alive disabled, the gzip internals were never cleaned up - fixed.

Costa Tsaousis (ktsaou) committed Apr 18, 2014 at 01:43 UTC d568fed16ff8d4e4eea16489b64078656aaca6ca
1 file changed +18 -7
netdata.c
+18 -7
@@ -2263,18 +2263,21 @@ void *new_client(void *ptr)
2263 }
2264 else if(!retval) {
2265 // timeout
2266 + web_client_reset(w);
2267 w->obsolete = 1;
2268 return NULL;
2269 }
2270
2271 if(FD_ISSET(w->ifd, &efds)) {
2272 debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket (%s).", w->id, strerror(errno));
2273 + web_client_reset(w);
2274 w->obsolete = 1;
2275 return NULL;
2276 }
2277
2278 if(FD_ISSET(w->ofd, &efds)) {
2279 debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket (%s).", w->id, strerror(errno));
2280 + web_client_reset(w);
2281 w->obsolete = 1;
2282 return NULL;
2283 }
@@ -2282,8 +2285,9 @@ void *new_client(void *ptr)
2285 if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
2286 if(web_client_send(w) < 0) {
2287 debug(D_WEB_CLIENT, "%llu: Closing client (input: %s).", w->id, strerror(errno));
2285 - errno = 0;
2288 + web_client_reset(w);
2289 w->obsolete = 1;
2290 + errno = 0;
2291 return NULL;
2292 }
2293 }
@@ -2291,8 +2295,9 @@ void *new_client(void *ptr)
2295 if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
2296 if(web_client_receive(w) < 0) {
2297 debug(D_WEB_CLIENT, "%llu: Closing client (output: %s).", w->id, strerror(errno));
2294 - errno = 0;
2298 + web_client_reset(w);
2299 w->obsolete = 1;
2300 + errno = 0;
2301 return NULL;
2302 }
2303
@@ -2320,7 +2325,11 @@ void syslog_allocations(void)
2325
2326 mi = mallinfo();
2327 if(mi.uordblks > mem) {
2323 - syslog(LOG_NOTICE, "Allocated memory increased from %d to %d (increased by %d bytes).", mem, mi.uordblks, mi.uordblks - mem );
2328 + int clients = 0;
2329 + struct web_client *w;
2330 + for(w = web_clients; w ; w = w->next) clients++;
2331 +
2332 + syslog(LOG_NOTICE, "Allocated memory increased from %d to %d (increased by %d bytes). There are %d web clients connected.", mem, mi.uordblks, mi.uordblks - mem, clients);
2333 mem = mi.uordblks;
2334 }
2335 /*
@@ -2354,8 +2363,8 @@ void *socket_listen_main(void *ptr)
2363 FD_ZERO (&efds);
2364
2365 for(;;) {
2357 - tv.tv_sec = 1;
2358 - tv.tv_usec = 0;
2366 + tv.tv_sec = 0;
2367 + tv.tv_usec = 200000;
2368
2369 FD_SET(listener, &ifds);
2370 FD_SET(listener, &efds);
@@ -2370,7 +2379,7 @@ void *socket_listen_main(void *ptr)
2379 else if(retval) {
2380 // check for new incoming connections
2381 if(FD_ISSET(listener, &ifds)) {
2373 - w = web_client_create(listener);
2382 + w = web_client_create(listener);
2383
2384 if(pthread_create(&w->thread, NULL, new_client, w) != 0)
2385 error("%llu: failed to create new thread.");
@@ -2381,14 +2390,16 @@ void *socket_listen_main(void *ptr)
2390 syslog(LOG_NOTICE, "%llu: %s connected", w->id, w->client_ip);
2391 }
2392 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
2393 +
2394 }
2385 - else debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
2395 + //else debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
2396
2397 // cleanup unused clients
2398 for(w = web_clients; w ; w = w?w->next:NULL) {
2399 if(w->obsolete) {
2400 syslog(LOG_NOTICE, "%llu: %s disconnected", w->id, w->client_ip);
2401 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
2402 + pthread_join(w->thread, NULL);
2403 w = web_client_free(w);
2404 syslog_allocations();
2405 }