Skip to content

Commit 71c2b75

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

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-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

+14-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def run_gc
573573
end
574574
expect do
575575
@client.query("SELECT SLEEP(1)")
576-
end.to raise_error(Mysql2::Error, /Lost connection to MySQL server/)
576+
end.to raise_error(Mysql2::Error, 'MySQL client is not connected')
577577

578578
if RUBY_PLATFORM !~ /mingw|mswin/
579579
expect do
@@ -582,6 +582,19 @@ def run_gc
582582
end
583583
end
584584

585+
it "should detect a closed connection" do
586+
connection_id = @client.thread_id
587+
Thread.new do
588+
sleep(0.1)
589+
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
590+
supervisor.query("KILL #{connection_id}")
591+
end.close
592+
end
593+
expect(@client.query("SELECT 1").first["1"]).to eql(1)
594+
sleep(0.2)
595+
expect(@client.closed?).to be true
596+
end
597+
585598
if RUBY_PLATFORM !~ /mingw|mswin/
586599
it "should not allow another query to be sent without fetching a result first" do
587600
@client.query("SELECT 1", async: true)

0 commit comments

Comments
 (0)