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

Error: This socket has been ended by the other party

Open hckhanh opened this issue 5 years ago • 4 comments

My database is in a VPN, so I need to connect to database through a tunnel. I try but receive this error:

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:455:14)
    at Channel.ondata (_stream_readable.js:716:22)
    at Channel.emit (events.js:315:20)
    at Channel.Readable.read (_stream_readable.js:505:10)
    at flow (_stream_readable.js:1005:34)
    at resume_ (_stream_readable.js:986:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EPIPE'
}

hckhanh avatar Sep 01 '20 09:09 hckhanh

At which stage do you see this error? ( at connection before even getting a query or sporadically during the life of connection )

sidorares avatar Sep 01 '20 09:09 sidorares

Hi @sidorares,

After entering the tunnel, I try to connect to the MySQL server. Here is my code:

const mysql = require("mysql2/promise");

module.exports.checkMysqlConnection = async function (credentials) {
  const connection = await mysql.createConnection(credentials);
  await connection.end();
};

So I think that I get this error at the create connection stage. I just wanna test the connection whether it succeeded or not. I ran this code inside a thread to make sure there is no port conflicting since I mapped to a random port.

hckhanh avatar Sep 01 '20 13:09 hckhanh

Still getting this error in staging/production today, latest version:

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:456:14)
    at Connection.write (/data/node_modules/mysql2/lib/connection.js:230:32)
    at Connection.writePacket (/data/node_modules/mysql2/lib/connection.js:279:12)
    at Quit.start (/data/node_modules/mysql2/lib/commands/quit.js:24:16)
    at Quit.execute (/data/node_modules/mysql2/lib/commands/command.js:39:22)
    at Connection.handlePacket (/data/node_modules/mysql2/lib/connection.js:425:32)
    at Connection.addCommand (/data/node_modules/mysql2/lib/connection.js:447:12)
    at Connection.end (/data/node_modules/mysql2/lib/connection.js:819:26)
    at /data/node_modules/mysql2/promise.js:128:23
    at new Promise (<anonymous>)
Emitted 'error' event on Connection instance at:
    at Connection._notifyError (/data/node_modules/mysql2/lib/connection.js:225:12)
    at Connection._handleFatalError (/data/node_modules/mysql2/lib/connection.js:156:10)
    at Connection._handleNetworkError (/data/node_modules/mysql2/lib/connection.js:169:10)
    at Socket.emit (events.js:376:20)
    at Socket.emit (domain.js:470:12)
    at emitErrorNT (net.js:1345:8)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 'EPIPE',
  fatal: true
}

Why not check for the connection being closed here? Obviously the error callback is ineffective in handling these low-level socket errors.

perry-mitchell avatar Jun 11 '21 06:06 perry-mitchell

Still getting these errors on occasional, out of the blue:

events.js:377
      throw er; // Unhandled 'error' event
      ^
Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:468:14)
    at Connection.write (/data/node_modules/mysql2/lib/connection.js:230:32)
    at Connection.writePacket (/data/node_modules/mysql2/lib/connection.js:279:12)
    at Quit.start (/data/node_modules/mysql2/lib/commands/quit.js:24:16)
    at Quit.execute (/data/node_modules/mysql2/lib/commands/command.js:39:22)
    at Connection.handlePacket (/data/node_modules/mysql2/lib/connection.js:425:32)
    at Connection.addCommand (/data/node_modules/mysql2/lib/connection.js:447:12)
    at Connection.end (/data/node_modules/mysql2/lib/connection.js:819:26)
    at /data/node_modules/mysql2/promise.js:128:23
    at new Promise (<anonymous>)
Emitted 'error' event on Connection instance at:
    at Connection._notifyError (/data/node_modules/mysql2/lib/connection.js:225:12)
    at Connection._handleFatalError (/data/node_modules/mysql2/lib/connection.js:156:10)
    at Connection._handleNetworkError (/data/node_modules/mysql2/lib/connection.js:169:10)
    at Socket.emit (events.js:400:28)
    at emitErrorNT (net.js:1358:8)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 'EPIPE',
  fatal: true
}

Is there no way to prevent these from being fatal? I'm fine with a connection/query throwing such an error, as I can catch it, but this takes out the whole service.

perry-mitchell avatar Oct 22 '21 05:10 perry-mitchell