Skip to content

Commit a7db7a9

Browse files
nyaxtjustincase
authored andcommitted
set active_thread for statement.execute
1 parent e771aa0 commit a7db7a9

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

ext/mysql2/client.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ static VALUE rb_hash_dup(VALUE other) {
3636
rb_raise(cMysql2Error, "MySQL connection is already open"); \
3737
}
3838

39-
#define MARK_CONN_INACTIVE(conn) \
40-
wrapper->active_thread = Qnil;
41-
4239
/*
4340
* compatability with mysql-connector-c, where LIBMYSQL_VERSION is the correct
4441
* variable to use, but MYSQL_SERVER_VERSION gives the correct numbers when
@@ -407,7 +404,7 @@ static VALUE do_send_query(void *args) {
407404
mysql_client_wrapper *wrapper = query_args->wrapper;
408405
if ((VALUE)rb_thread_call_without_gvl(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
409406
/* an error occurred, we're not active anymore */
410-
MARK_CONN_INACTIVE(self);
407+
wrapper->active_thread = Qnil;
411408
return rb_raise_mysql2_error(wrapper);
412409
}
413410
return Qnil;
@@ -590,6 +587,25 @@ static VALUE finish_and_mark_inactive(void *args) {
590587
}
591588
#endif
592589

590+
void rb_mysql_client_set_active_thread(VALUE self) {
591+
VALUE thread_current = rb_thread_current();
592+
GET_CLIENT(self);
593+
594+
// see if this connection is still waiting on a result from a previous query
595+
if (NIL_P(wrapper->active_thread)) {
596+
// mark this connection active
597+
wrapper->active_thread = thread_current;
598+
} else if (wrapper->active_thread == thread_current) {
599+
rb_raise(cMysql2Error, "This connection is still waiting for a result, try again once you have the result");
600+
} else {
601+
VALUE inspect = rb_inspect(wrapper->active_thread);
602+
const char *thr = StringValueCStr(inspect);
603+
604+
rb_raise(cMysql2Error, "This connection is in use by: %s", thr);
605+
RB_GC_GUARD(inspect);
606+
}
607+
}
608+
593609
/* call-seq:
594610
* client.abandon_results!
595611
*
@@ -633,7 +649,6 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
633649
struct nogvl_send_query_args args;
634650
int async = 0;
635651
VALUE opts, current;
636-
VALUE thread_current = rb_thread_current();
637652
#ifdef HAVE_RUBY_ENCODING_H
638653
rb_encoding *conn_enc;
639654
#endif
@@ -663,23 +678,10 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
663678
#endif
664679
args.sql_ptr = StringValuePtr(args.sql);
665680
args.sql_len = RSTRING_LEN(args.sql);
666-
667-
/* see if this connection is still waiting on a result from a previous query */
668-
if (NIL_P(wrapper->active_thread)) {
669-
/* mark this connection active */
670-
wrapper->active_thread = thread_current;
671-
} else if (wrapper->active_thread == thread_current) {
672-
rb_raise(cMysql2Error, "This connection is still waiting for a result, try again once you have the result");
673-
} else {
674-
VALUE inspect = rb_inspect(wrapper->active_thread);
675-
const char *thr = StringValueCStr(inspect);
676-
677-
rb_raise(cMysql2Error, "This connection is in use by: %s", thr);
678-
RB_GC_GUARD(inspect);
679-
}
680-
681681
args.wrapper = wrapper;
682682

683+
rb_mysql_client_set_active_thread(self);
684+
683685
#ifndef _WIN32
684686
rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0);
685687

ext/mysql2/client.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ typedef struct {
5656
rb_raise(cMysql2Error, "closed MySQL connection"); \
5757
}
5858

59+
void rb_mysql_client_set_active_thread(VALUE self);
60+
61+
#define MARK_CONN_INACTIVE(conn) do {\
62+
GET_CLIENT(conn); \
63+
wrapper->active_thread = Qnil; \
64+
} while(0)
65+
5966
#define GET_CLIENT(self) \
6067
mysql_client_wrapper *wrapper; \
6168
Data_Get_Struct(self, mysql_client_wrapper, wrapper);

ext/mysql2/result.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ static unsigned int msec_char_to_uint(char *msec_char, size_t len)
211211

212212
static void rb_mysql_result_alloc_result_buffers(VALUE self, MYSQL_FIELD *fields) {
213213
unsigned int i;
214-
VALUE rowVal;
215214
mysql2_result_wrapper * wrapper;
216215
GetMysql2Result(self, wrapper);
217216

ext/mysql2/statement.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void *nogvl_execute(void *ptr) {
135135
xfree(bind_buffers[i].buffer); \
136136
} \
137137
} \
138-
xfree(bind_buffers);
138+
if (argc > 0) xfree(bind_buffers);
139139

140140
/* call-seq: stmt.execute
141141
*
@@ -266,15 +266,15 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
266266
rb_raise(cMysql2Error, "%s", mysql_stmt_error(stmt));
267267
}
268268

269-
if (bind_count > 0) {
270-
FREE_BINDS;
271-
}
269+
FREE_BINDS;
272270

273271
metadata = mysql_stmt_result_metadata(stmt);
274272
if(metadata == NULL) {
275273
if(mysql_stmt_errno(stmt) != 0) {
276-
// FIXME: MARK_CONN_INACTIVE(wrapper->client);
277-
// FIXME: rb_raise_mysql2_stmt_error(self);
274+
// either CR_OUT_OF_MEMORY or CR_UNKNOWN_ERROR. both fatal.
275+
276+
MARK_CONN_INACTIVE(stmt_wrapper->client);
277+
rb_raise(cMysql2Error, "%s", mysql_stmt_error(stmt));
278278
}
279279
// no data and no error, so query was not a SELECT
280280
return Qnil;
@@ -288,6 +288,7 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
288288
if (mysql_stmt_store_result(stmt)) {
289289
rb_raise(cMysql2Error, "%s", mysql_stmt_error(stmt));
290290
}
291+
MARK_CONN_INACTIVE(stmt_wrapper->client);
291292

292293
resultObj = rb_mysql_result_to_obj(stmt_wrapper->client, wrapper->encoding, current, metadata, stmt);
293294

0 commit comments

Comments
 (0)