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

Minor optimizations , fixed memory leaks and invalid reads #4229

Merged
merged 6 commits into from
May 30, 2023
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
7 changes: 4 additions & 3 deletions include/MySQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ class ProxySQL_Poll {
void expand(unsigned int more);

public:
unsigned int poll_timeout;
unsigned long loops;
StatCounters *loop_counters;
unsigned int len;
unsigned int size;
struct pollfd *fds;
MySQL_Data_Stream **myds;
unsigned long long *last_recv;
unsigned long long *last_sent;
std::atomic<bool> bootstrapping_listeners;
volatile int pending_listener_add;
volatile int pending_listener_del;
unsigned int poll_timeout;
unsigned long loops;
StatCounters *loop_counters;

ProxySQL_Poll();
~ProxySQL_Poll();
Expand Down
4 changes: 4 additions & 0 deletions lib/MySQL_PreparedStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ MySQL_STMT_Manager_v14::MySQL_STMT_Manager_v14() {
}

MySQL_STMT_Manager_v14::~MySQL_STMT_Manager_v14() {
for (auto it = map_stmt_id_to_info.begin(); it != map_stmt_id_to_info.end(); ++it) {
MySQL_STMT_Global_info * a = it->second;
delete a;
}
}

void MySQL_STMT_Manager_v14::ref_count_client(uint64_t _stmt_id ,int _v, bool lock) {
Expand Down
2 changes: 1 addition & 1 deletion lib/MySQL_Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,7 @@ bool MySQL_Protocol::process_pkt_handshake_response(unsigned char *pkt, unsigned
}
}
proxy_debug(PROXY_DEBUG_MYSQL_PROTOCOL,1,"Handshake (%s auth) <user:\"%s\" pass:\"%s\" db:\"%s\" max_pkt:%u>, capabilities:%u char:%u, use_ssl:%s\n",
(capabilities & CLIENT_SECURE_CONNECTION ? "new" : "old"), user, tmp_pass, db, max_pkt, capabilities, charset, ((*myds)->encrypted ? "yes" : "no"));
(capabilities & CLIENT_SECURE_CONNECTION ? "new" : "old"), user, tmp_pass, db, (*myds)->myconn->options.max_allowed_pkt, capabilities, charset, ((*myds)->encrypted ? "yes" : "no"));
free(tmp_pass);
}
#endif
Expand Down
20 changes: 12 additions & 8 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7488,7 +7488,9 @@ void MySQL_Session::RequestEnd(MySQL_Data_Stream *myds) {
// if a prepared statement is executed, LogQuery was already called
break;
default:
LogQuery(myds);
if (session_fast_forward==false) {
LogQuery(myds);
}
break;
}

Expand All @@ -7502,13 +7504,15 @@ void MySQL_Session::RequestEnd(MySQL_Data_Stream *myds) {
}
myds->free_mysql_real_query();
}
// reset status of the session
status=WAITING_CLIENT_DATA;
if (client_myds) {
// reset status of client data stream
client_myds->DSS=STATE_SLEEP;
// finalize the query
CurrentQuery.end();
if (session_fast_forward==false) {
// reset status of the session
status=WAITING_CLIENT_DATA;
if (client_myds) {
// reset status of client data stream
client_myds->DSS=STATE_SLEEP;
// finalize the query
CurrentQuery.end();
}
}
started_sending_data_to_client=false;
previous_hostgroup = current_hostgroup;
Expand Down
46 changes: 28 additions & 18 deletions lib/MySQL_Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ ProxySQL_Poll::ProxySQL_Poll() {
len=0;
pending_listener_add=0;
pending_listener_del=0;
bootstrapping_listeners = true;
size=MIN_POLL_LEN;
fds=(struct pollfd *)malloc(size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)malloc(size*sizeof(MySQL_Data_Stream *));
Expand Down Expand Up @@ -3198,18 +3199,25 @@ void MySQL_Thread::run() {
#endif // IDLE_THREADS

pthread_mutex_unlock(&thread_mutex);
while ( // spin here if ...
(n=__sync_add_and_fetch(&mypolls.pending_listener_add,0)) // there is a new listener to add
||
(GloMTH->bootstrapping_listeners == true) // MySQL_Thread_Handlers has more listeners to configure
) {
if (n) {
poll_listener_add(n);
assert(__sync_bool_compare_and_swap(&mypolls.pending_listener_add,n,0));
}
if (unlikely(mypolls.bootstrapping_listeners == true)) {
while ( // spin here if ...
(n=__sync_add_and_fetch(&mypolls.pending_listener_add,0)) // there is a new listener to add
||
(GloMTH->bootstrapping_listeners == true) // MySQL_Thread_Handlers has more listeners to configure
) {
if (n) {
poll_listener_add(n);
assert(__sync_bool_compare_and_swap(&mypolls.pending_listener_add,n,0));
} else {
if (GloMTH->bootstrapping_listeners == false) {
// we stop looping
mypolls.bootstrapping_listeners = false;
}
}
#ifdef DEBUG
usleep(5+rand()%10);
usleep(5+rand()%10);
#endif
}
}

proxy_debug(PROXY_DEBUG_NET, 7, "poll_timeout=%u\n", mypolls.poll_timeout);
Expand Down Expand Up @@ -3243,17 +3251,19 @@ void MySQL_Thread::run() {
}
#endif // IDLE_THREADS

while ((n=__sync_add_and_fetch(&mypolls.pending_listener_del,0))) { // spin here
if (static_cast<int>(n) == -1) {
for (unsigned int i = 0; i < mypolls.len; i++) {
if (mypolls.myds[i] && mypolls.myds[i]->myds_type == MYDS_LISTENER) {
poll_listener_del(mypolls.myds[i]->fd);
if (unlikely(maintenance_loop == true)) {
while ((n=__sync_add_and_fetch(&mypolls.pending_listener_del,0))) { // spin here
if (static_cast<int>(n) == -1) {
for (unsigned int i = 0; i < mypolls.len; i++) {
if (mypolls.myds[i] && mypolls.myds[i]->myds_type == MYDS_LISTENER) {
poll_listener_del(mypolls.myds[i]->fd);
}
}
} else {
poll_listener_del(n);
}
} else {
poll_listener_del(n);
assert(__sync_bool_compare_and_swap(&mypolls.pending_listener_del,n,0));
}
assert(__sync_bool_compare_and_swap(&mypolls.pending_listener_del,n,0));
}

pthread_mutex_lock(&thread_mutex);
Expand Down
3 changes: 2 additions & 1 deletion lib/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ void proxy_debug_func(enum debug_module module, int verbosity, int thr, const ch
rc=(*proxy_sqlite3_bind_text)(statement1, 11, longdebugbuff2, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, db);
SAFE_SQLITE3_STEP2(statement1);
rc=(*proxy_sqlite3_clear_bindings)(statement1); ASSERT_SQLITE_OK(rc, db);
rc=(*proxy_sqlite3_reset)(statement1); ASSERT_SQLITE_OK(rc, db);
// Note: no assert() in proxy_debug_func() after sqlite3_reset() because it is possible that we are in shutdown
rc=(*proxy_sqlite3_reset)(statement1); // ASSERT_SQLITE_OK(rc, db);
}
}
pthread_mutex_unlock(&debug_mutex);
Expand Down
29 changes: 15 additions & 14 deletions src/SQLite3_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,21 +1015,22 @@ static void *child_mysql(void *arg) {
}
}
sess->to_process=1;
// Get and set the client address before the sesion is processed.
union {
struct sockaddr_in in;
struct sockaddr_in6 in6;
} custom_sockaddr;
struct sockaddr *addr=(struct sockaddr *)malloc(sizeof(custom_sockaddr));
socklen_t addrlen=sizeof(custom_sockaddr);
memset(addr, 0, sizeof(custom_sockaddr));
sess->client_myds->client_addrlen=addrlen;
sess->client_myds->client_addr=addr;
int g_rc = getpeername(sess->client_myds->fd, addr, &addrlen);
if (g_rc == -1) {
proxy_error("'getpeername' failed with error: %d\n", g_rc);
if (sess->client_myds->client_addr == NULL) {
// Get and set the client address before the sesion is processed.
union {
struct sockaddr_in in;
struct sockaddr_in6 in6;
} custom_sockaddr;
struct sockaddr *addr=(struct sockaddr *)malloc(sizeof(custom_sockaddr));
socklen_t addrlen=sizeof(custom_sockaddr);
memset(addr, 0, sizeof(custom_sockaddr));
sess->client_myds->client_addrlen=addrlen;
sess->client_myds->client_addr=addr;
int g_rc = getpeername(sess->client_myds->fd, addr, &addrlen);
if (g_rc == -1) {
proxy_error("'getpeername' failed with error: %d\n", g_rc);
}
}

int rc=sess->handler();
if (rc==-1) goto __exit_child_mysql;
}
Expand Down