@@ -46,6 +46,7 @@ struct nogvl_connect_args {
46
46
struct nogvl_send_query_args {
47
47
MYSQL * mysql ;
48
48
VALUE sql ;
49
+ mysql_client_wrapper * wrapper ;
49
50
};
50
51
51
52
/*
@@ -250,6 +251,17 @@ static VALUE nogvl_send_query(void *ptr) {
250
251
return rv == 0 ? Qtrue : Qfalse ;
251
252
}
252
253
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
+
253
265
/*
254
266
* even though we did rb_thread_select before calling this, a large
255
267
* 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) {
419
431
REQUIRE_OPEN_DB (wrapper );
420
432
args .mysql = wrapper -> client ;
421
433
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
- }
429
434
430
435
defaults = rb_iv_get (self , "@query_options" );
431
436
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) {
446
451
args .sql = rb_str_export_to_enc (args .sql , conn_enc );
447
452
#endif
448
453
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" );
453
460
}
454
461
462
+ args .wrapper = wrapper ;
463
+ rb_rescue2 (do_send_query , (VALUE )& args , disconnect_and_raise , self , rb_eException , (VALUE )0 );
464
+
455
465
#ifndef _WIN32
456
466
if (!async ) {
457
467
async_args .fd = wrapper -> client -> net .fd ;
0 commit comments