Skip to content

Commit ba6bb52

Browse files
committed
Use vio_is_connected to check connected state
1 parent 98304cb commit ba6bb52

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

ext/mysql2/client.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
2626
}
2727

2828
#if defined(HAVE_MYSQL_NET_VIO) || defined(HAVE_ST_NET_VIO)
29-
#define CONNECTED(wrapper) (wrapper->client->net.vio != NULL && wrapper->client->net.fd != -1)
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))
3031
#elif defined(HAVE_MYSQL_NET_PVIO) || defined(HAVE_ST_NET_PVIO)
3132
#define CONNECTED(wrapper) (wrapper->client->net.pvio != NULL && wrapper->client->net.fd != -1)
3233
#endif

spec/mysql2/client_spec.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,19 @@ def run_gc
342342
@client.close
343343
expect(@client.closed?).to eql(true)
344344
end
345+
346+
it "should detect a closed connection" do
347+
connection_id = @client.thread_id
348+
Thread.new do
349+
sleep(0.1)
350+
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
351+
supervisor.query("KILL #{connection_id}")
352+
end.close
353+
end
354+
expect(@client.query("SELECT 1").first["1"]).to eql(1)
355+
sleep(0.2)
356+
expect(@client.closed?).to be true
357+
end
345358
end
346359

347360
it "should not try to query closed mysql connection" do
@@ -573,7 +586,7 @@ def run_gc
573586
end
574587
expect do
575588
@client.query("SELECT SLEEP(1)")
576-
end.to raise_error(Mysql2::Error, /Lost connection to MySQL server/)
589+
end.to raise_error(Mysql2::Error, 'MySQL client is not connected')
577590

578591
if RUBY_PLATFORM !~ /mingw|mswin/
579592
expect do
@@ -582,6 +595,19 @@ def run_gc
582595
end
583596
end
584597

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+
585611
if RUBY_PLATFORM !~ /mingw|mswin/
586612
it "should not allow another query to be sent without fetching a result first" do
587613
@client.query("SELECT 1", async: true)

0 commit comments

Comments
 (0)