node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Process crash/end when do pool.query without internet

Open ferogot opened this issue 3 years ago • 2 comments

Hi everyone, I have error (with process end):

events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: connect EHOSTUNREACH 45.67.57.43:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on PoolConnection instance at:
    at PoolConnection._notifyError (D:\repo\resources\node_modules\mysql2\lib\connection.js:230:12)
    at PoolConnection._handleFatalError (D:\repo\resources\node_modules\mysql2\lib\connection.js:156:10)
    at PoolConnection._handleNetworkError (D:\repo\resources\node_modules\mysql2\lib\connection.js:169:10)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  errno: -4073,
  code: 'EHOSTUNREACH',
  syscall: 'connect',
  address: <>,
  port: <>,
  fatal: true
}

Also I see others error codes which crash process, trace was same.

This error appear when i start .query() requests without any internet on my PC. More precisely on first query.

I tried all try/catch methods: pool.on('error'...), connection.on('error'...), try/catch with async/await syntax, .catch on query promise. But nothing catch it.

After debugging mysql2 coded I found the problem and possibly fix, atleast it fix crashes without internet :) Need to replace this._notifyError call to this._handleFatalError in listener below:

https://github.com/sidorares/node-mysql2/blob/3e52aff9aad5959c2810825065cbee7029611328/lib/connection.js#L94-L108

In current lib version connection._notifyError will be called twice in my case, first one works perfectly, but second will crash process (it is same connection object, I add new prop to connection in first call and see same prop in second call). Also I noticed what connection was released between calls (this._pool == null) and connection.listenerCount('error') changed 2 -> 0.

ferogot avatar Mar 15 '22 15:03 ferogot

I missed something important. The reason for replacing _notifyError with _handleFatalError is fatal = true in the error object. So I guess we need to check _protocolError.fatal and do the right handling

ferogot avatar Mar 15 '22 16:03 ferogot

I'm able to catch the ENETUNREACH, ENOTFOUND, and ER_ACCESS_DENIED_ERROR errors using the code shown here. The API returns the expected JSON error without crashing or ending. I'm unable to get the error code EHOSTUNREACH even when I turn off my internet while running the test but I'd assume the code would catch it too.

flipperlite avatar Mar 21 '22 20:03 flipperlite