@@ -183,7 +183,11 @@ bool MysqlConnection::RealConnect(const char* hostname,
183
183
}
184
184
185
185
void MysqlConnection::Close () {
186
+ DEBUG_PRINTF (" Close: pthread_mutex_lock\n " );
187
+ pthread_mutex_lock (&this ->query_lock );
188
+ DEBUG_PRINTF (" Close: pthread_mutex_lock'ed\n " );
186
189
if (this ->_conn ) {
190
+
187
191
mysql_close (this ->_conn );
188
192
this ->_conn = NULL ;
189
193
this ->connected = false ;
@@ -192,6 +196,8 @@ void MysqlConnection::Close() {
192
196
this ->connect_errno = 0 ;
193
197
this ->connect_error = NULL ;
194
198
}
199
+ DEBUG_PRINTF (" Close: pthread_mutex_unlock\n " );
200
+ pthread_mutex_unlock (&this ->query_lock );
195
201
}
196
202
197
203
MysqlConnection::MysqlConnection (): ObjectWrap() {
@@ -990,8 +996,15 @@ void MysqlConnection::EIO_After_Query(uv_work_t *req) {
990
996
// for both MysqlResult creation and callback call
991
997
int argc = 1 ; // node.js convention, there is always at least one argument for callback
992
998
Local<Value> argv[3 ];
993
-
994
- if (!query_req->ok ) {
999
+ DEBUG_PRINTF (" EIO_After_Query: in\n " );
1000
+ if (!query_req->conn ->_conn || !query_req->conn ->connected || query_req->connection_closed ) {
1001
+ DEBUG_PRINTF (" EIO_After_Query: !query_req->conn->_conn || !query_req->conn->connected || query_req->connection_closed\n " );
1002
+ // Check connection
1003
+ // If closeSync() is called after query(),
1004
+ // than connection is destroyed here
1005
+ // https://github.com/Sannis/node-mysql-libmysqlclient/issues/157
1006
+ argv[0 ] = V8EXC (" Connection is closed by closeSync() during query" );
1007
+ } else if (!query_req->ok ) {
995
1008
unsigned int error_string_length = strlen (query_req->error ) + 20 ;
996
1009
char * error_string = new char [error_string_length];
997
1010
snprintf (error_string, error_string_length, " Query error #%d: %s" ,
@@ -1021,6 +1034,7 @@ void MysqlConnection::EIO_After_Query(uv_work_t *req) {
1021
1034
}
1022
1035
1023
1036
if (query_req->callback ->IsFunction ()) {
1037
+ DEBUG_PRINTF (" EIO_After_Query: node::MakeCallback\n " );
1024
1038
node::MakeCallback (
1025
1039
Context::GetCurrent ()->Global (),
1026
1040
Persistent<Function>::Cast (query_req->callback ),
@@ -1030,7 +1044,13 @@ void MysqlConnection::EIO_After_Query(uv_work_t *req) {
1030
1044
query_req->callback .Dispose ();
1031
1045
}
1032
1046
1033
- query_req->conn ->Unref ();
1047
+ // See comment above
1048
+ DEBUG_PRINTF (" EIO_After_Query: Unref?\n " );
1049
+ if (!query_req->conn ->_conn || !query_req->conn ->connected ) {
1050
+ DEBUG_PRINTF (" EIO_After_Query: Unref\n " );
1051
+ query_req->conn ->Unref ();
1052
+ DEBUG_PRINTF (" EIO_After_Query: Unref'ed\n " );
1053
+ }
1034
1054
1035
1055
delete[] query_req->query ;
1036
1056
delete query_req;
@@ -1043,14 +1063,27 @@ void MysqlConnection::EIO_Query(uv_work_t *req) {
1043
1063
1044
1064
MysqlConnection *conn = query_req->conn ;
1045
1065
1046
- if (!conn->_conn ) {
1066
+ DEBUG_PRINTF (" EIO_Query: pthread_mutex_lock\n " );
1067
+ pthread_mutex_lock (&conn->query_lock );
1068
+ DEBUG_PRINTF (" EIO_Query: pthread_mutex_lock'ed\n " );
1069
+
1070
+ // Check connection
1071
+ // If closeSync() is called after query(),
1072
+ // than connection is destroyed here
1073
+ // https://github.com/Sannis/node-mysql-libmysqlclient/issues/157
1074
+ if (!conn->_conn || !conn->connected ) {
1047
1075
query_req->ok = false ;
1048
- // TODO: Handle this
1076
+ query_req->connection_closed = true ;
1077
+
1078
+ DEBUG_PRINTF (" EIO_Query: !conn->_conn || !conn->connected\n " );
1079
+ DEBUG_PRINTF (" EIO_Query: pthread_mutex_unlock\n " );
1080
+ pthread_mutex_unlock (&conn->query_lock );
1081
+ return ;
1049
1082
}
1083
+ query_req->connection_closed = false ;
1050
1084
1051
1085
MYSQLCONN_DISABLE_MQ;
1052
1086
1053
- pthread_mutex_lock (&conn->query_lock );
1054
1087
int r = mysql_real_query (conn->_conn , query_req->query , query_req->query_len );
1055
1088
int errno = mysql_errno (conn->_conn );
1056
1089
if (r != 0 || errno != 0 ) {
@@ -1085,6 +1118,7 @@ void MysqlConnection::EIO_Query(uv_work_t *req) {
1085
1118
}
1086
1119
}
1087
1120
}
1121
+ DEBUG_PRINTF (" EIO_Query: pthread_mutex_unlock\n " );
1088
1122
pthread_mutex_unlock (&conn->query_lock );
1089
1123
}
1090
1124
0 commit comments