sequelize/sequelize — pull request #1244 by iuriaranda (2014-01-13 10:08:02+00:00)
Title: New queries hang when a client connection is closed after the 100 ms period of inactivity

I'm experiencing some issues with queries that hang up randomly. I'm developing a RESTful API with sequelize (using MySQL) and until now I have not experienced any problems at all. Last month we updated sequelize to the latest stable version (1.6.0) and now when I run our API test suite (which is predictable and constant), there is a specific test case that hangs up sometimes. I've tracked down the issue and I think I've found the problem (or problems).

The thing is that sequelize closes a connection after 100 milliseconds of inactivity, all good so far. But when a query is issued after that 100 ms period of inactivity, it happens to be sent to the mysql module after calling the `client.end()` but before the callback is called in the `connector-manager.js` file.
```
client.end(function() {
if (!self.useQueue) {
return client.destroy();
}

var intervalObj = null
var cleanup = function () {
var retryCt = 0
// make sure to let client finish before calling destroy
if (self && self.hasQueuedItems) {
return
}
// needed to prevent mysql connection leak
client.destroy()
if (self && self.client) {
self.client = null
}
clearInterval(intervalObj)
}
intervalObj = setInterval(cleanup, 10)
cleanup()
return
})
```

The thing is that in this short period of time a query may be issued and the client instance is still available in the connection-manager, but it is already closed to new queries. In this situation, the mysql module emits an error in the `run` query method, but due to the fact that all the process since the query is issued until the `run` method is called is synchronous, the query issuer doesn't get the change to subscribe to the query event emitter, so there is a "hang up" effect.

This only happens when pooling is disabled.
I've included a test case and a possible fix.