-
Notifications
You must be signed in to change notification settings - Fork 550
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
Use vio_is_connected to check connected state #1022
base: master
Are you sure you want to change the base?
Conversation
71c2b75
to
f52cb3b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Bunch of comments in the review. Really appreciate the detailed notes in the PR message.
ext/mysql2/client.c
Outdated
@@ -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); | |||
#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) |
There was a problem hiding this comment.
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).
spec/mysql2/client_spec.rb
Outdated
@@ -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') |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
ext/mysql2/client.c
Outdated
@@ -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); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
Thanks for the quick review @sodabrew! Looking at CI, there are a bunch of compatibility issues between versions which I'm going to investigate. I'll ping once I'm ready for another look. |
944c15c
to
c3bd301
Compare
c3bd301
to
914f56b
Compare
Can you take another look @sodabrew? I'm not super happy with this, but I can't really come up with a better way. The problem is that Another option would be to duplicate the A couple of the CI test environments are failing, but the failures don't seem related to these changes. |
Thinking about |
@kirs I think removing |
@sodabrew Would you be able to take another look? |
Problem
The
CONNECTED
define which is used by theclosed?
method, does not detect if the server has remotely closed the connection. In this case, should the remote side terminate the connection, the call toclosed?
will incorrectly returntrue
. As a result, it's possible for subsequent query to raise an exception once the client actually detects that the connection has been closed.Solution
Use the
vio_is_connected
function to detect if the connection has been closed. The implementation will perform a non-blocking read on the socket to determine if the connection has been closed.This function has been available since at least MySQL 5.6.x (from what I've Googled), so should be reliable. The symbol is globally visible as part of libmysqlclient shared library, but is not declared in the libmysqlclient headers (which is why I had to declare it inline). It does not seem as though the API has changed between 5.6 -> 8.0, so I feel safe using it here.