Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection close race condition #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/driver.js
Expand Up @@ -493,14 +493,30 @@ ConnectionInPool.prototype.connect = function(callback) {
}
Connection.call(this, this._options);
Connection.prototype.connect.call(this, function(err) {
self.connected = !err;
if (err) {
self.connected = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not self.setDisconnected()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
else {
self.setConnected();
}

self.unhealthyAt = err ? new Date().getTime() : 0;
callback(err);
});
};

ConnectionInPool.prototype.setConnected = function() {
this.connected = true;
this.emit('connected');
};

ConnectionInPool.prototype.setDisconnected = function() {
this.connected = false;
this.emit('disconnected');
};

ConnectionInPool.prototype.isHealthy = function() {
return this.unhealthyAt === 0;
return this.unhealthyAt === 0;
}

/**
Expand Down Expand Up @@ -596,7 +612,7 @@ PooledConnection.prototype._incr = function() {
} else if (this.connections[this.current_node].isStaleUnhealthy()) {
// unhealthy and stale, so let reset the node (appears as if unconnected).
this.connections[this.current_node].taken = false;
this.connections[this.current_node].connected = false;
this.connections[this.current_node].setDisconnected();
this.connections[this.current_node].unhealthyAt = 0;
break;
} else {
Expand Down Expand Up @@ -808,6 +824,9 @@ PooledConnection.prototype._closeConnections = function(closeCallback) {
if (con.connected) {
con.close(cb);
} else {
con.on('connected', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in cb being called twice, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks.

Fixed in 5b6ff40

con.close(null);
});
cb(null);
}
}, function(err) {
Expand Down
17 changes: 15 additions & 2 deletions test/test_driver.js
Expand Up @@ -881,8 +881,8 @@ exports.testLearnStepTimeout = function(test, assert) {
server.listen(8688, '127.0.0.1', callback);
},

function executeQueryPooledConnection(callback) {
conn.execute('UPDATE CfUgly SET A=1 WHERE KEY=1', [], function(err) {
function executeQueryPooledConnection(callback) {
conn.execute('UPDATE CfUgly SET A=1 WHERE KEY=1', [], function(err) {
assert.ifError(err);
callback();
});
Expand Down Expand Up @@ -1084,6 +1084,7 @@ exports.testPooledConnectionShutdown = function(test, assert) {
});
};


exports.testPooledConnectionShutdownTwice = function(test, assert) {
var hosts = ['127.0.0.1:9160'];
var conn = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1'});
Expand Down Expand Up @@ -1117,3 +1118,15 @@ exports.testPooledConnectionShutdownTwice = function(test, assert) {
secondCbCalledImmediatelyWithError = true;
});
};


exports.testPooledContainerImmediateShutdown = function(test, assert) {
var hosts = ['127.0.0.1:9160'];
var pool = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1'});

pool.connect();
pool.shutdown(function(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this test have failed outright previously or just timed out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It times out. Wasn't sure how to make it fail outright.

assert.ifError(err);
test.finish();
});
};