Skip to content

Commit 944c15c

Browse files
committed
Check for vio_is_connected export
1 parent f52cb3b commit 944c15c

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

Diff for: ext/mysql2/client.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
2525
rb_raise(cMysql2Error, "MySQL client is not initialized"); \
2626
}
2727

28+
#if defined(HAVE_VIO_IS_CONNECTED)
29+
my_bool vio_is_connected(Vio *vio);
30+
#define IO_IS_CONNECTED(wrapper) vio_is_connected(wrapper->client->net.vio)
31+
#else
32+
#define IO_IS_CONNECTED(wrapper)
33+
#endif
34+
2835
#if defined(HAVE_MYSQL_NET_VIO) || defined(HAVE_ST_NET_VIO)
29-
my_bool vio_is_connected(Vio *vio);
30-
#define CONNECTED(wrapper) (wrapper->client->net.vio != NULL && wrapper->client->net.fd != -1 && vio_is_connected(wrapper->client->net.vio))
36+
#define CONNECTED(wrapper) (wrapper->client->net.vio != NULL && wrapper->client->net.fd != -1 && IO_IS_CONNECTED(wrapper))
3137
#elif defined(HAVE_MYSQL_NET_PVIO) || defined(HAVE_ST_NET_PVIO)
32-
#define CONNECTED(wrapper) (wrapper->client->net.pvio != NULL && wrapper->client->net.fd != -1)
38+
#define CONNECTED(wrapper) (wrapper->client->net.pvio != NULL && wrapper->client->net.fd != -1 && IO_IS_CONNECTED(wrapper))
3339
#endif
3440

3541
#define REQUIRE_CONNECTED(wrapper) \
@@ -1366,6 +1372,14 @@ static VALUE initialize_ext(VALUE self) {
13661372
return self;
13671373
}
13681374

1375+
static VALUE rb_vio_is_connected(VALUE self) {
1376+
#if defined(HAVE_VIO_IS_CONNECTED)
1377+
return Qtrue;
1378+
#else
1379+
return Qfalse;
1380+
#endif
1381+
}
1382+
13691383
/* call-seq: client.prepare # => Mysql2::Statement
13701384
*
13711385
* Create a new prepared statement.
@@ -1455,6 +1469,8 @@ void init_mysql2_client() {
14551469
rb_define_private_method(cMysql2Client, "connect", rb_mysql_connect, 8);
14561470
rb_define_private_method(cMysql2Client, "_query", rb_mysql_query, 2);
14571471

1472+
rb_define_private_method(cMysql2Client, "_vio_is_connected?", rb_vio_is_connected, 0);
1473+
14581474
sym_id = ID2SYM(rb_intern("id"));
14591475
sym_version = ID2SYM(rb_intern("version"));
14601476
sym_header_version = ID2SYM(rb_intern("header_version"));

Diff for: ext/mysql2/extconf.rb

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def add_ssl_defines(header)
106106
add_ssl_defines(mysql_h)
107107
have_struct_member('MYSQL', 'net.vio', mysql_h)
108108
have_struct_member('MYSQL', 'net.pvio', mysql_h)
109+
have_func('vio_is_connected')
109110

110111
# These constants are actually enums, so they cannot be detected by #ifdef in C code.
111112
have_const('MYSQL_ENABLE_CLEARTEXT_PLUGIN', mysql_h)

Diff for: spec/mysql2/client_spec.rb

+2-14
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ def run_gc
344344
end
345345

346346
it "should detect a closed connection" do
347+
skip "libmysqlclient does not export vio_is_connected()" unless @client.send(:_vio_is_connected?)
347348
connection_id = @client.thread_id
348349
Thread.new do
349350
sleep(0.1)
@@ -586,7 +587,7 @@ def run_gc
586587
end
587588
expect do
588589
@client.query("SELECT SLEEP(1)")
589-
end.to raise_error(Mysql2::Error, 'MySQL client is not connected')
590+
end.to raise_error(Mysql2::Error)
590591

591592
if RUBY_PLATFORM !~ /mingw|mswin/
592593
expect do
@@ -595,19 +596,6 @@ def run_gc
595596
end
596597
end
597598

598-
it "should detect a closed connection" do
599-
connection_id = @client.thread_id
600-
Thread.new do
601-
sleep(0.1)
602-
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
603-
supervisor.query("KILL #{connection_id}")
604-
end.close
605-
end
606-
expect(@client.query("SELECT 1").first["1"]).to eql(1)
607-
sleep(0.2)
608-
expect(@client.closed?).to be true
609-
end
610-
611599
if RUBY_PLATFORM !~ /mingw|mswin/
612600
it "should not allow another query to be sent without fetching a result first" do
613601
@client.query("SELECT 1", async: true)

0 commit comments

Comments
 (0)