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.connectTimeout is not cleared when connect error happens

See original GitHub issue

Timeout connectTimeout which is created in Connection constructor (node_modules/mysql2/lib/connection.js) is not cleared when error happens. It should be cleared for example in callback _handleNetworkError.

Run this without mysqld listening on 127.0.0.1:3306:

let mysql = require("mysql2");
let connection = mysql.createConnection({});
connection.on("error", err => console.log(new Date(), "error", err))
process.on("exit", () => console.log(new Date(), "exit"));

Possible output:

2019-08-06T20:05:27.571Z error Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}
2019-08-06T20:05:37.573Z exit

Notice that it takes 10 seconds to exit which is default for connect timeout.

Without error event handler on repl.it:

let mysql = require("mysql2");
let connection = mysql.createConnection({});
Process crashed with: Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
Process crashed with: Error: Connection lost: The server closed the connection.
    at Socket.Connection.stream.on (/home/runner/node_modules/mysql2/lib/connection.js:88:31)
    at Socket.emit (events.js:198:13)
    at TCP._handle.close (net.js:606:12)
Process crashed with: Error: connect ETIMEDOUT
    at Connection._handleTimeoutError (/home/runner/node_modules/mysql2/lib/connection.js:171:17)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

This change solves this problem: Before:

  _handleNetworkError(err) {
    // Do not throw an error when a connection ends with a RST,ACK packet
    if (err.errno === 'ECONNRESET' && this._closing) {
      return;
    }
    this._handleFatalError(err);
  }

After:

  _handleNetworkError(err) {
    if (this.connectTimeout) {
      Timers.clearTimeout(this.connectTimeout);
      this.connectTimeout = null;
    }
    // Do not throw an error when a connection ends with a RST,ACK packet
    if (err.errno === 'ECONNRESET' && this._closing) {
      return;
    }
    this._handleFatalError(err);
  }

Now it exits immediately:

2019-08-06T20:11:31.459Z error Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}
2019-08-06T20:11:31.464Z exit

Maybe it should be cleared also in _handleFatalError.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
misos1commented, Aug 14, 2019

@dvisztempacct It is basically leak. What if someone uses really long timeout? These leaked timeouts can add up and cause real problems. It is like leaking files by not closing stream created by fs.createReadStream.

1reaction
jacobbogerscommented, Dec 3, 2019

I will do PR, i need this fixed in my project

Read more comments on GitHub >

github_iconTop Results From Across the Web

[SOLVED] How to Fix the ERR_CONNECTION_TIMED_OUT ...
Ways to Fix Err_Connection_Timed_Out Error. Method 1: Try VPN; Method 2: Flush DNS Cache; Method 3: Check Your Connection; Method 4: Clear ...
Read more >
11 Ways to Fix the ERR_CONNECTION_TIMED_OUT Error
A data request error occurs when a server refuses to send obtained data back to the computer. As a result, the network connection...
Read more >
How to Fix the Connection Timeout Error on Steam
Click on “Clear downloads” in the next window. Click ” Confirm” to finish the process. Once you have done all these steps, try...
Read more >
FIX: The Connect Timeout property is not applied correctly ...
Resolves a problem in which the Connect Timeout property is not applied correctly when the connection should be available in the pool in...
Read more >
Connection timed out" exception occur when URL is up?
You cant do much besides setting a timeout and adding good error messages (especially printing out the hosts' resolved address). If you want...
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