Permalink
Browse files

Handle closeSync() during querySend()

Refs #157, #159
  • Loading branch information...
1 parent 622f8e4 commit 61d998c314a4bd047b8f5c4e0220baea1c19db5f @Sannis committed Sep 9, 2012
Showing with 35 additions and 1 deletion.
  1. +17 −0 src/mysql_bindings_connection.cc
  2. +18 −1 tests/issues/test-issue-157.js
@@ -1182,6 +1182,23 @@ void MysqlConnection::EV_After_QuerySend(NODE_ADDON_SHIM_IO_WATCH_CALLBACK_ARGUM
MysqlConnection *conn = query_req->conn;
+ // Check connection
+ // If closeSync() is called after query(),
+ // than connection is destroyed here
+ // https://github.com/Sannis/node-mysql-libmysqlclient/issues/157
+ if (!conn->_conn || !conn->connected) {
+ query_req->ok = false;
+ query_req->connection_closed = true;
+
+ DEBUG_PRINTF("EV_After_QuerySend: !conn->_conn || !conn->connected\n");
+
+ // The callback part, just call the existing code
+ EIO_After_Query(_req);
+
+ return;
+ }
+ query_req->connection_closed = false;
+
// The query part, mostly same with EIO_Query
// TODO merge the same parts with EIO_Query
int r = mysql_read_query_result(conn->_conn);
@@ -8,7 +8,7 @@ See license text in LICENSE file
// Load configuration
var cfg = require("../config");
-exports.issue157 = function (test) {
+exports.issue157_query = function (test) {
test.expect(3);
var conn = cfg.mysql_libmysqlclient.createConnectionSync(cfg.host, cfg.user, cfg.password, cfg.database);
@@ -24,3 +24,20 @@ exports.issue157 = function (test) {
test.ok(true);
};
+
+exports.issue157_querySend = function (test) {
+ test.expect(3);
+
+ var conn = cfg.mysql_libmysqlclient.createConnectionSync(cfg.host, cfg.user, cfg.password, cfg.database);
+
+ conn.querySend('SELECT SLEEP(100);', function (err) {
+ test.ok(err instanceof Error);
+ test.ok(err.message.indexOf("Connection is closed by closeSync()") === 0);
+
+ test.done();
+ });
+
+ conn.closeSync();
+
+ test.ok(true);
+};

0 comments on commit 61d998c

Please sign in to comment.