mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
366 stars 91 forks source link

Aborted connection when using ioredis and process.exit(0) #216

Open brenc opened 1 year ago

brenc commented 1 year ago

I'm not sure if this is a bug with this module or what but I thought I'd start by making note of this very strange issue here for future reference. Using Node v16.17.0 and mariadb v3.0.1 inside a Docker container (node:16-bullseye-slim).

Consider the following ESM:

import Redis from 'ioredis';
import mariadb from 'mariadb';
// import { sleep } from 'modern-async';

const pool = mariadb.createPool({
  connectionLimit: 10,
  database: 'mydb',
  host,
  multipleStatements: true,
  user,
  password,
  trace: true,
});

const redis = new Redis({
  host: process.env.REDIS_HOST,
});

async function main() {
  const queueLen = await redis.llen('queue');
  console.log(`queue length: ${queueLen}`);
}

await main();
await pool.end();
await redis.quit();
// await sleep(100);
process.exit(0);

On exit this causes MariaDB errors like: [Warning] Aborted connection 632 to db: 'unconnected' user: 'unauthenticated' host: '172.26.0.12' (This connection closed normally without authentication)

It looks like the following conditions have to be met:

This set of conditions causes aborted connection errors when the script exits.

Workarounds:

rusher commented 1 year ago

This is strange, because this kind of error occurs when initiating connection to mariadb server, Server send a 'greeting packet', and connector never sending anything back to server. Server close socket after some time with this type of error.

I would check that there is no firewall between your client and server. (and ensure that assumption with some wireshark logs)

brenc commented 1 year ago

Yes, very strange. This is all on the internal network. No firewalls. I set up and manage the entire environment so I know for sure.

Like I said, it only happens when those conditions are met. Sometimes the connection does get connected and authenticated and I'll see logs like this as well:

[Warning] Aborted connection 36 to db: 'mydb' user: 'mydb' host: '192.168.0.13' (Got an error reading communication packets)

MR4online commented 1 year ago

we do have a similar setup and similar errors. After moving from mysql2 we have massive problems with pooled connections.


Not sure if following fits here or in #239 or in a new bug report.

I currently think pool.end() does not close all connections, because, when I Query show status like '%onn%'; I get Results like: _Abortedconnects: 0 Connections: 10

While "Connections" is rising on every service restart with the number of connectionLimit: 10. No closed connections or decreasing connection count, even after minutes.

May be the pool only closes active connections, not idle ones?