Skip to content

Use vio_is_connected to check connected state #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ Style/TrailingCommaInLiteral:

Style/TrivialAccessors:
AllowPredicates: true

Metrics/BlockLength:
Exclude:
- 'benchmark/setup_db.rb'
- '**/*.rake'
- 'spec/spec_helper.rb'
- 'spec/**/*_spec.rb'
5 changes: 0 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ Layout/IndentHeredoc:
Metrics/AbcSize:
Max: 90

# Offense count: 31
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 860

# Offense count: 1
# Configuration parameters: CountBlocks.
Metrics/BlockNesting:
Expand Down
3 changes: 2 additions & 1 deletion ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
}

#if defined(HAVE_MYSQL_NET_VIO) || defined(HAVE_ST_NET_VIO)
#define CONNECTED(wrapper) (wrapper->client->net.vio != NULL && wrapper->client->net.fd != -1)
my_bool vio_is_connected(Vio *vio);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to a header file and add an autoconf test that checks if the header is available in the given client library (there are more client libraries than just libmysqlclient).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Yeah, there's some symbol resolution issues I'm seeing in CI. I'm going to add some autoconf-style checks to clean this up.

#define CONNECTED(wrapper) (wrapper->client->net.vio != NULL && wrapper->client->net.fd != -1 && vio_is_connected(wrapper->client->net.vio))
#elif defined(HAVE_MYSQL_NET_PVIO) || defined(HAVE_ST_NET_PVIO)
#define CONNECTED(wrapper) (wrapper->client->net.pvio != NULL && wrapper->client->net.fd != -1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the vio_is_connected call here as well, or a reason why it's not there (such as, and I don't know the truth of this, if net.pvio is older versions that don't have this function in the library at all).

#endif
Expand Down
28 changes: 27 additions & 1 deletion spec/mysql2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,19 @@ def run_gc
@client.close
expect(@client.closed?).to eql(true)
end

it "should detect a closed connection" do
connection_id = @client.thread_id
Thread.new do
sleep(0.1)
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
supervisor.query("KILL #{connection_id}")
end.close
end
expect(@client.query("SELECT 1").first["1"]).to eql(1)
sleep(0.2)
expect(@client.closed?).to be true
end
end

it "should not try to query closed mysql connection" do
Expand Down Expand Up @@ -573,7 +586,7 @@ def run_gc
end
expect do
@client.query("SELECT SLEEP(1)")
end.to raise_error(Mysql2::Error, /Lost connection to MySQL server/)
end.to raise_error(Mysql2::Error, 'MySQL client is not connected')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the different error text?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because we're validating the connection before executing the query (and it's now failing) and it raises with this error text.


if RUBY_PLATFORM !~ /mingw|mswin/
expect do
Expand All @@ -582,6 +595,19 @@ def run_gc
end
end

it "should detect a closed connection" do
connection_id = @client.thread_id
Thread.new do
sleep(0.1)
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
supervisor.query("KILL #{connection_id}")
end.close
end
expect(@client.query("SELECT 1").first["1"]).to eql(1)
sleep(0.2)
expect(@client.closed?).to be true
end

if RUBY_PLATFORM !~ /mingw|mswin/
it "should not allow another query to be sent without fetching a result first" do
@client.query("SELECT 1", async: true)
Expand Down