File tree Expand file tree Collapse file tree 4 files changed +27
-18
lines changed Expand file tree Collapse file tree 4 files changed +27
-18
lines changed Original file line number Diff line number Diff line change @@ -196,10 +196,16 @@ static void *nogvl_close(void *ptr) {
196
196
197
197
static void rb_mysql_client_free (void * ptr ) {
198
198
mysql_client_wrapper * wrapper = (mysql_client_wrapper * )ptr ;
199
+ decr_mysql2_client (wrapper );
200
+ }
199
201
202
+ void decr_mysql2_client (mysql_client_wrapper * wrapper )
203
+ {
200
204
wrapper -> refcount -- ;
201
205
if (wrapper -> refcount == 0 ) {
202
- close_connection_and_free_mysql2_client (wrapper );
206
+ nogvl_close (wrapper );
207
+ xfree (wrapper -> client );
208
+ xfree (wrapper );
203
209
}
204
210
}
205
211
@@ -1309,12 +1315,3 @@ void init_mysql2_client() {
1309
1315
LONG2NUM (CLIENT_BASIC_FLAGS ));
1310
1316
#endif
1311
1317
}
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
-
Original file line number Diff line number Diff line change @@ -36,9 +36,6 @@ rb_thread_call_without_gvl(
36
36
#endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
37
37
#endif /* ! HAVE_RB_THREAD_CALL_WITHOUT_GVL */
38
38
39
- void init_mysql2_client ();
40
- void close_connection_and_free_mysql2_client (void * );
41
-
42
39
typedef struct {
43
40
VALUE encoding ;
44
41
VALUE active_thread ; /* rb_thread_current() or Qnil */
@@ -51,4 +48,7 @@ typedef struct {
51
48
MYSQL * client ;
52
49
} mysql_client_wrapper ;
53
50
51
+ void init_mysql2_client ();
52
+ void decr_mysql2_client (mysql_client_wrapper * wrapper );
53
+
54
54
#endif
Original file line number Diff line number Diff line change 1
1
#include <mysql2_ext.h>
2
2
#include <stdint.h>
3
3
4
- #include "client.h"
5
4
#include "mysql_enc_to_ruby.h"
6
5
7
6
#ifdef HAVE_RUBY_ENCODING_H
@@ -85,10 +84,7 @@ static void rb_mysql_result_free(void *ptr) {
85
84
86
85
// If the GC gets to client first it will be nil
87
86
if (wrapper -> client != Qnil ) {
88
- wrapper -> client_wrapper -> refcount -- ;
89
- if (wrapper -> client_wrapper -> refcount == 0 ) {
90
- close_connection_and_free_mysql2_client (wrapper -> client_wrapper );
91
- }
87
+ decr_mysql2_client (wrapper -> client_wrapper );
92
88
}
93
89
94
90
xfree (wrapper );
Original file line number Diff line number Diff line change @@ -104,6 +104,22 @@ def connect *args
104
104
ssl_client . close
105
105
end
106
106
107
+ it "should not leave dangling connections after garbage collection" do
108
+ GC . start
109
+ client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
110
+ before_count = client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
111
+
112
+ 10 . times do
113
+ Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] ) . query ( 'SELECT 1' )
114
+ end
115
+ after_count = client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
116
+ after_count . should == before_count + 10
117
+
118
+ GC . start
119
+ final_count = client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
120
+ final_count . should == before_count
121
+ end
122
+
107
123
it "should be able to connect to database with numeric-only name" do
108
124
lambda {
109
125
creds = DatabaseCredentials [ 'numericuser' ]
You can’t perform that action at this time.
0 commit comments