@@ -420,6 +420,26 @@ static VALUE allocate(VALUE klass) {
420420 return obj ;
421421}
422422
423+ static void rb_mysql_client_set_active_fiber (VALUE self , bool async_check ) {
424+ VALUE fiber_current = rb_fiber_current ();
425+ GET_CLIENT (self );
426+
427+ // see if this connection is still waiting on a result from a previous query
428+ if (NIL_P (wrapper -> active_fiber )) {
429+ // mark this connection active
430+ wrapper -> active_fiber = fiber_current ;
431+ } else if (wrapper -> active_fiber == fiber_current ) {
432+ if (async_check ) {
433+ rb_raise (cMysql2Error , "This connection is still waiting for a result, try again once you have the result" );
434+ }
435+ } else {
436+ VALUE inspect = rb_inspect (wrapper -> active_fiber );
437+ const char * thr = StringValueCStr (inspect );
438+
439+ rb_raise (cMysql2Error , "This connection is in use by: %s" , thr );
440+ }
441+ }
442+
423443/* call-seq:
424444 * Mysql2::Client.escape(string)
425445 *
@@ -571,11 +591,14 @@ static VALUE rb_mysql_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VA
571591 */
572592static VALUE rb_mysql_client_close (VALUE self ) {
573593 GET_CLIENT (self );
594+ rb_mysql_client_set_active_fiber (self , false);
574595
575596 if (wrapper -> client ) {
576597 rb_thread_call_without_gvl (nogvl_close , wrapper , RUBY_UBF_IO , 0 );
577598 }
578599
600+ wrapper -> active_fiber = Qnil ;
601+
579602 return Qnil ;
580603}
581604
@@ -798,24 +821,6 @@ static VALUE disconnect_and_mark_inactive(VALUE self) {
798821 return Qnil ;
799822}
800823
801- static void rb_mysql_client_set_active_fiber (VALUE self ) {
802- VALUE fiber_current = rb_fiber_current ();
803- GET_CLIENT (self );
804-
805- // see if this connection is still waiting on a result from a previous query
806- if (NIL_P (wrapper -> active_fiber )) {
807- // mark this connection active
808- wrapper -> active_fiber = fiber_current ;
809- } else if (wrapper -> active_fiber == fiber_current ) {
810- rb_raise (cMysql2Error , "This connection is still waiting for a result, try again once you have the result" );
811- } else {
812- VALUE inspect = rb_inspect (wrapper -> active_fiber );
813- const char * thr = StringValueCStr (inspect );
814-
815- rb_raise (cMysql2Error , "This connection is in use by: %s" , thr );
816- }
817- }
818-
819824/* call-seq:
820825 * client.abandon_results!
821826 *
@@ -873,7 +878,7 @@ static VALUE rb_mysql_query(VALUE self, VALUE sql, VALUE current) {
873878 args .sql_len = RSTRING_LEN (args .sql );
874879 args .wrapper = wrapper ;
875880
876- rb_mysql_client_set_active_fiber (self );
881+ rb_mysql_client_set_active_fiber (self , true );
877882
878883#ifndef _WIN32
879884 rb_rescue2 (do_send_query , (VALUE )& args , disconnect_and_raise , self , rb_eException , (VALUE )0 );
@@ -1233,12 +1238,16 @@ static void *nogvl_ping(void *ptr) {
12331238 */
12341239static VALUE rb_mysql_client_ping (VALUE self ) {
12351240 GET_CLIENT (self );
1241+ rb_mysql_client_set_active_fiber (self , true);
12361242
1243+ VALUE result = Qnil ;
12371244 if (!CONNECTED (wrapper )) {
1238- return Qfalse ;
1245+ result = Qfalse ;
12391246 } else {
1240- return (VALUE )rb_thread_call_without_gvl (nogvl_ping , wrapper -> client , RUBY_UBF_IO , 0 );
1247+ result = (VALUE )rb_thread_call_without_gvl (nogvl_ping , wrapper -> client , RUBY_UBF_IO , 0 );
12411248 }
1249+ wrapper -> active_fiber = Qnil ;
1250+ return result ;
12421251}
12431252
12441253/* call-seq:
0 commit comments