Skip to content

Commit aecbde5

Browse files
committed
mark the connection active as late as possible
1 parent cd682ea commit aecbde5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

ext/mysql2/client.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct nogvl_connect_args {
4646
struct nogvl_send_query_args {
4747
MYSQL *mysql;
4848
VALUE sql;
49+
mysql_client_wrapper *wrapper;
4950
};
5051

5152
/*
@@ -250,6 +251,17 @@ static VALUE nogvl_send_query(void *ptr) {
250251
return rv == 0 ? Qtrue : Qfalse;
251252
}
252253

254+
static VALUE do_send_query(void *args) {
255+
struct nogvl_send_query_args *query_args = args;
256+
mysql_client_wrapper *wrapper = query_args->wrapper;
257+
if (rb_thread_blocking_region(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
258+
// an error occurred, we're not active anymore
259+
MARK_CONN_INACTIVE(self);
260+
return rb_raise_mysql2_error(wrapper);
261+
}
262+
return Qnil;
263+
}
264+
253265
/*
254266
* even though we did rb_thread_select before calling this, a large
255267
* response can overflow the socket buffers and cause us to eventually
@@ -419,13 +431,6 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
419431
REQUIRE_OPEN_DB(wrapper);
420432
args.mysql = wrapper->client;
421433

422-
// see if this connection is still waiting on a result from a previous query
423-
if (wrapper->active == 0) {
424-
// mark this connection active
425-
wrapper->active = 1;
426-
} else {
427-
rb_raise(cMysql2Error, "This connection is still waiting for a result, try again once you have the result");
428-
}
429434

430435
defaults = rb_iv_get(self, "@query_options");
431436
if (rb_scan_args(argc, argv, "11", &args.sql, &opts) == 2) {
@@ -446,12 +451,17 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
446451
args.sql = rb_str_export_to_enc(args.sql, conn_enc);
447452
#endif
448453

449-
if (rb_thread_blocking_region(nogvl_send_query, &args, RUBY_UBF_IO, 0) == Qfalse) {
450-
// an error occurred, we're not active anymore
451-
MARK_CONN_INACTIVE(self);
452-
return rb_raise_mysql2_error(wrapper);
454+
// see if this connection is still waiting on a result from a previous query
455+
if (wrapper->active == 0) {
456+
// mark this connection active
457+
wrapper->active = 1;
458+
} else {
459+
rb_raise(cMysql2Error, "This connection is still waiting for a result, try again once you have the result");
453460
}
454461

462+
args.wrapper = wrapper;
463+
rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0);
464+
455465
#ifndef _WIN32
456466
if (!async) {
457467
async_args.fd = wrapper->client->net.fd;

0 commit comments

Comments
 (0)