Skip to content

Commit c514179

Browse files
committed
Merge pull request #444 from rlineweaver/close-connection-on-result-gc
Close MySQL connection on result GC
2 parents 6a1169d + 997e426 commit c514179

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

ext/mysql2/client.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,7 @@ static void rb_mysql_client_free(void *ptr) {
199199

200200
wrapper->refcount--;
201201
if (wrapper->refcount == 0) {
202-
nogvl_close(wrapper);
203-
xfree(wrapper->client);
204-
xfree(wrapper);
202+
close_connection_and_free_mysql2_client(wrapper);
205203
}
206204
}
207205

@@ -1311,3 +1309,12 @@ void init_mysql2_client() {
13111309
LONG2NUM(CLIENT_BASIC_FLAGS));
13121310
#endif
13131311
}
1312+
1313+
void close_connection_and_free_mysql2_client(void *ptr) {
1314+
mysql_client_wrapper *wrapper = (mysql_client_wrapper *)ptr;
1315+
1316+
nogvl_close(wrapper);
1317+
xfree(wrapper->client);
1318+
xfree(wrapper);
1319+
}
1320+

ext/mysql2/client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ rb_thread_call_without_gvl(
3737
#endif /* ! HAVE_RB_THREAD_CALL_WITHOUT_GVL */
3838

3939
void init_mysql2_client();
40+
void close_connection_and_free_mysql2_client(void *);
4041

4142
typedef struct {
4243
VALUE encoding;

ext/mysql2/result.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <mysql2_ext.h>
22
#include <stdint.h>
33

4+
#include "client.h"
45
#include "mysql_enc_to_ruby.h"
56

67
#ifdef HAVE_RUBY_ENCODING_H
@@ -86,8 +87,7 @@ static void rb_mysql_result_free(void *ptr) {
8687
if (wrapper->client != Qnil) {
8788
wrapper->client_wrapper->refcount--;
8889
if (wrapper->client_wrapper->refcount == 0) {
89-
xfree(wrapper->client_wrapper->client);
90-
xfree(wrapper->client_wrapper);
90+
close_connection_and_free_mysql2_client(wrapper->client_wrapper);
9191
}
9292
}
9393

0 commit comments

Comments
 (0)