question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection.connect callback may never be executed

See original GitHub issue

I’ve found a subtle inconsistency between mysql and mysql2…

mysql separates the creation of a Connection object and the establishment of a database connection, i.e.

var mysql = require('mysql');

// Create the connection object
var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

// Actually establish a database connection
connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});

However mysql2 both creates the Connection object and establishes the database connection in the Connection constructor, emitting a connect event when successful.

mysql2 attempts to maintain compatibility with mysql by adding a connect function, which listens for the connect event and executes the callback, however this creates a potential race condition where if the connect event is emitted before connection.connect(cb) is called, the callback will never be executed. The following code demonstrates the problem…

const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
});

connection.on('connect', () => {
  console.log('Connection established');

  connection.connect(() => {
    console.log('Connect callback executed'); // <------ NEVER REACHED
  });
})

setTimeout(() => {
  console.log('Timeout');
  process.exit(1);
}, 5000);
$ node index.js 
Connection established
Timeout

I discovered this problem while trying to switch marv-mysql-driver to mysql2. I can easily rewrite my code to not go async between initiating the connection and calling connect, but thought it was worth mentioning, as it could affect other applications, and could be fixed by executing the callback immediately if the connection was currently active, or with an error if the connection had already been closed.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sidorarescommented, Apr 4, 2020

Either close or break the connection (not sure how yet)

One way of doing this is to create “MITM” stream and use it as custom transport via “stream” option - https://github.com/sidorares/node-mysql2/blob/master/documentation/Extras.md#connecting-using-custom-stream That way you don’t need to control real server to kill the connection

Alternatively use createServer() and actually kill connection from server side, in that case both client and server live in the same node process

1reaction
cressie176commented, Apr 4, 2020

Will do

Read more comments on GitHub >

github_iconTop Results From Across the Web

connection.query callback never executed · Issue #202 - GitHub
I tracked problem to connection.query method from mysql2 library,even though query executed its callback never executed and program just exits.
Read more >
Nodejs 12 Callback not working for mysql connection?
The function you are passing to the connection.connect is called a callback function. what that mean is, it will be called only after...
Read more >
What counts as a "Failed Callback Attempt" - Amazon Connect
A failed callback attempt would be along the lines of: an agent accepts a callback but then something goes wrong between then and...
Read more >
3 Getting Database Connections in UCP - Oracle Help Center
This callback enables applications and frameworks to initialize connections retrieved from Universal Connection Pool. It is executed at every connection ...
Read more >
Troubleshoot common connection issues to Azure SQL ...
These connection problems can be caused by reconfiguration, firewall settings, a connection timeout, incorrect login information, or failure to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found