From 5e8a2a7e3f6d07c0f8bd1d34dd38c4884f8fd093 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Fri, 15 Nov 2019 10:54:02 +0000 Subject: [PATCH 1/9] Fix mem leak in idle threads --- lib/MySQL_Thread.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 7dee7b204..50b06ca80 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -3252,7 +3252,8 @@ void MySQL_Threads_Handler::init(unsigned int num, size_t stack) { assert(rc==0); mysql_threads=(proxysql_mysql_thread_t *)calloc(num_threads,sizeof(proxysql_mysql_thread_t)); #ifdef IDLE_THREADS - mysql_threads_idles=(proxysql_mysql_thread_t *)calloc(num_threads,sizeof(proxysql_mysql_thread_t)); + if (GloVars.global.idle_threads) + mysql_threads_idles=(proxysql_mysql_thread_t *)calloc(num_threads,sizeof(proxysql_mysql_thread_t)); #endif // IDLE_THREADS } @@ -3264,9 +3265,11 @@ proxysql_mysql_thread_t * MySQL_Threads_Handler::create_thread(unsigned int tn, } #ifdef IDLE_THREADS } else { - if (pthread_create(&mysql_threads_idles[tn].thread_id, &attr, start_routine , &mysql_threads_idles[tn]) != 0) { - proxy_error("Thread creation\n"); - assert(0); + if (GloVars.global.idle_threads) { + if (pthread_create(&mysql_threads_idles[tn].thread_id, &attr, start_routine , &mysql_threads_idles[tn]) != 0) { + proxy_error("Thread creation\n"); + assert(0); + } } #endif // IDLE_THREADS } @@ -5550,7 +5553,7 @@ SQLite3_result * MySQL_Threads_Handler::SQL3_Processlist() { thr=(MySQL_Thread *)mysql_threads[i].worker; #ifdef IDLE_THREADS } else { - if (mysql_thread___session_idle_show_processlist && mysql_threads_idles) { + if (GloVars.global.idle_threads && mysql_thread___session_idle_show_processlist && mysql_threads_idles) { thr=(MySQL_Thread *)mysql_threads_idles[i-num_threads].worker; } #endif // IDLE_THREADS From f193c2f94adc88183d600dd47414b8fc97ab17b8 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Fri, 15 Nov 2019 20:15:51 +0000 Subject: [PATCH 2/9] Fix mem leak in MySQL_Listeners_Manager --- lib/MySQL_Thread.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 50b06ca80..ad831dd28 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -267,12 +267,18 @@ int MySQL_Listeners_Manager::add(const char *iface, unsigned int num_threads, in #else s = ( atoi(port) ? listen_on_port(address, atoi(port), PROXYSQL_LISTEN_LEN) : listen_on_unix(address, PROXYSQL_LISTEN_LEN)); #endif /* SO_REUSEPORT */ - if (s==-1) return s; + if (s==-1) { + free(address); + free(port); + return s; + } if (s>0) { ioctl_FIONBIO(s,1); iface_info *ifi=new iface_info((char *)iface, address, atoi(port), s); ifaces->add(ifi); } + free(address); + free(port); return s; } From 41459e4c70c1e2740297f5c8e03640ff833aedd6 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Mon, 25 Nov 2019 09:01:23 +0000 Subject: [PATCH 3/9] Fix memory leak in MySQL_HostGroups_Manager --- lib/MySQL_HostGroups_Manager.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 9cd219485..d59a02bed 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -1053,6 +1053,8 @@ MySQL_HostGroups_Manager::~MySQL_HostGroups_Manager() { if (admindb) { delete admindb; } + for (auto info : AWS_Aurora_Info_Map) + delete info.second; #ifdef MHM_PTHREAD_MUTEX pthread_mutex_destroy(&lock); #endif From 2aac7c317985823d7311f922b1b4b3adf65fcff9 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sat, 30 Nov 2019 09:20:14 +0000 Subject: [PATCH 4/9] Fix mem leak in Monitor connections pool --- lib/MySQL_Monitor.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index 32b5ee0e4..0293587b8 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -140,15 +140,23 @@ class MonMySrvC { public: char *address; uint16_t port; - PtrArray *conns; + std::unique_ptr conns; MonMySrvC(char *a, uint16_t p) { address = strdup(a); port = p; - conns = new PtrArray(); + conns = std::unique_ptr(new PtrArray()); }; ~MonMySrvC() { free(address); - delete conns; + if (conns) { + while (conns->len) { + MYSQL* mysql = static_cast(conns->index(0)); + if (mysql) { + mysql_close(mysql); mysql=NULL; + } + conns->remove_index_fast(0); + } + } } }; @@ -172,6 +180,17 @@ public: pthread_mutex_init(&m2, NULL); #endif // DEBUG }; + ~MySQL_Monitor_Connection_Pool() { + if (servers) { + while(servers->len) { + MonMySrvC *srv = static_cast(servers->index(0)); + if (srv) { + delete srv; + } + servers->remove_index_fast(0); + } + } + } void conn_register(MySQL_Monitor_State_Data *mmsd) { #ifdef DEBUG std::lock_guard lock(mutex); From 14bfc4db779449edeb1426cc69582e37206165c0 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sat, 30 Nov 2019 18:59:16 +0000 Subject: [PATCH 5/9] Fix addrinfo leak --- lib/network.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/network.cpp b/lib/network.cpp index d573e9144..adbba27c3 100644 --- a/lib/network.cpp +++ b/lib/network.cpp @@ -65,6 +65,7 @@ int listen_on_port(char *ip, uint16_t port, int backlog, bool reuseport) { } } + freeaddrinfo(ai); // return the socket return sd; } From 0227afaee7f54fa258569b74a648d5eda70f2de4 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sat, 30 Nov 2019 19:13:19 +0000 Subject: [PATCH 6/9] Fix HGM gtid_ev_async leak --- lib/MySQL_HostGroups_Manager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index d59a02bed..12f503af4 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -620,7 +620,6 @@ static void * GTID_syncer_run() { proxy_error("could not initialise GTID sync loop\n"); exit(EXIT_FAILURE); } - MyHGM->gtid_ev_async = (struct ev_async *)malloc(sizeof(struct ev_async)); //ev_async_init(gtid_ev_async, gtid_async_cb); //ev_async_start(gtid_ev_loop, gtid_ev_async); MyHGM->gtid_ev_timer = (struct ev_timer *)malloc(sizeof(struct ev_timer)); @@ -1040,7 +1039,6 @@ void MySQL_HostGroups_Manager::shutdown() { ev_async_send(gtid_ev_loop, gtid_ev_async); GTID_syncer_thread->join(); delete GTID_syncer_thread; - free(gtid_ev_async); } MySQL_HostGroups_Manager::~MySQL_HostGroups_Manager() { @@ -1055,6 +1053,7 @@ MySQL_HostGroups_Manager::~MySQL_HostGroups_Manager() { } for (auto info : AWS_Aurora_Info_Map) delete info.second; + free(gtid_ev_async); #ifdef MHM_PTHREAD_MUTEX pthread_mutex_destroy(&lock); #endif From 3a169beb537cc063763bb423796af6268d483dd5 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sun, 1 Dec 2019 08:49:48 +0000 Subject: [PATCH 7/9] Fix latest_version leak --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.cpp b/src/main.cpp index 8ff00a746..690af39d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -213,6 +213,7 @@ void * main_check_latest_version_thread(void *arg) { if (latest_version) { proxy_info("Latest ProxySQL version available: %s\n", latest_version); } + free(latest_version); return NULL; } From 6a6873b342d826d234024cd39a06c02d6d2b592e Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sun, 1 Dec 2019 16:34:57 +0000 Subject: [PATCH 8/9] Fix leaks in logger and admin --- lib/MySQL_Logger.cpp | 4 ++++ lib/ProxySQL_Admin.cpp | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/MySQL_Logger.cpp b/lib/MySQL_Logger.cpp index 06cf1dfc2..6e52a7bbb 100644 --- a/lib/MySQL_Logger.cpp +++ b/lib/MySQL_Logger.cpp @@ -584,6 +584,8 @@ void MySQL_Logger::events_set_base_filename() { } void MySQL_Logger::events_set_datadir(char *s) { + if (events.datadir) + free(events.datadir); events.datadir=strdup(s); flush_log(); }; @@ -612,6 +614,8 @@ void MySQL_Logger::audit_set_base_filename() { } void MySQL_Logger::audit_set_datadir(char *s) { + if (audit.datadir) + free(audit.datadir); audit.datadir=strdup(s); flush_log(); }; diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 2ec872752..f0b363ac4 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -5033,6 +5033,7 @@ void ProxySQL_Admin::flush_admin_variables___database_to_runtime(SQLite3DB *db, sprintf(q,"DELETE FROM global_variables WHERE variable_name=\"admin-%s\"",r->fields[0]); db->execute(q); } + free(val); } } else { proxy_debug(PROXY_DEBUG_ADMIN, 4, "Set variable %s with value \"%s\"\n", r->fields[0],r->fields[1]); From 19e7c3f1e265e51fa1690e476995c72a11172cf4 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sun, 1 Dec 2019 20:07:44 +0000 Subject: [PATCH 9/9] Fix memory leak in MySQL_HostGroups_Manager (gtid) --- lib/MySQL_HostGroups_Manager.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 12f503af4..f1eb40e7a 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -1006,6 +1006,8 @@ MySQL_HostGroups_Manager::MySQL_HostGroups_Manager() { incoming_aws_aurora_hostgroups = NULL; pthread_rwlock_init(>id_rwlock, NULL); gtid_missing_nodes = false; + gtid_ev_loop=NULL; + gtid_ev_timer=NULL; gtid_ev_async = (struct ev_async *)malloc(sizeof(struct ev_async)); { @@ -1054,6 +1056,10 @@ MySQL_HostGroups_Manager::~MySQL_HostGroups_Manager() { for (auto info : AWS_Aurora_Info_Map) delete info.second; free(gtid_ev_async); + if (gtid_ev_loop) + ev_loop_destroy(gtid_ev_loop); + if (gtid_ev_timer) + free(gtid_ev_timer); #ifdef MHM_PTHREAD_MUTEX pthread_mutex_destroy(&lock); #endif