2

I'm testing MySQL locally with Node, and every time I run the file, the program doesn't end unless I manually terminate it (with ctrl+c). I searched online, and I especially looked at this question (not a duplicate—it doesn't ask about my error), which said that the program will end if I call pool.end(). However, when I call the function, it gives me an error: Pool is closed, which leads me to believe that the pool is already closed. But then why doesn't my program end? My code looks something like this:

// database.js
const mysql = require("mysql2");

const {HOST, USER, DATABASE, PASSWORD} = process.env;
const pool = mysql.createPool({
    host: HOST,
    user: USER,
    database: DATABASE,
    password: PASSWORD,
    waitForConnections: true,
    connectionLimit: 50,
    queueLimit: 0
});

exports.set = data => {
  return new Promise((resolve, reject) => {
    pool.execute("INSERT INTO `mytable` (name, email) VALUES (?, ?);", [data.name, data.email], function(err) {
      if(err) reject(err);
      else resolve();
    });
  });
};

exports.get = data => {
  return new Promise((resolve, reject) => {
    pool.query("SELECT * FROM `mytable` WHERE `name`=? AND `email`=?;", [data.name, data.email], function(err, result) {
      if(err) reject(err);
      else resolve(result);
    });
  });
};

exports.getAll = () => {
  return new Promise((resolve, reject) => {
    pool.query("SELECT * FROM `mytable`;", function(err, result) {
      if(err) reject(err);
      else resolve(result);
    });
  });
};

exports.end = () => {
  pool.end();
};

And then in my main file:

const db = require("./database");

(async () => {
  const data = { name: "John Doe", email: "johndoe@example.com" };
  await db.set(data);
  const result = await db.get(data);
  console.log(result);
  db.end(); // I added this line as mentioned in https://stackoverflow.com/questions/61976788/nodejs-program-not-exiting-after-completion
})();

It gives the error Pool is closed with a stack trace. If I don't include the db.end() line the program just won't end (at the moment db.end() doesn't solve the problem either). Does anyone know the solution?

Update

As suggested in the comments, I installed a library called why-is-node-still-running to assess the situation, but the logs didn't make much sense. Here are the logs for reference:

There are 6 handle(s) keeping the process running:

TCPWRAP
C:\[REDACTED]\node_modules\mysql
2\lib\connection.js:45     - this.stream = Net.connect(
C:\[REDACTED]\node_modules\mysql
2\lib\pool_connection.js:7 - super(options);
C:\[REDACTED]\node_modules\mysql
2\lib\pool.js:51           - connection = new PoolConnection(this, {
C:\[REDACTED]\node_modules\mysql
2\lib\pool.js:139          - this.getConnection((err, conn) => {
C:\[REDACTED]\database.js:39
                           - pool.query("SELECT * FROM `registration`", function
 (err, result) {

DNSCHANNEL
(unknown stack trace)

GETADDRINFOREQWRAP

Update 2

When I put db.end() inside setTimeout() (without a provided delay number), setImmediate(), or process.nextTick() (in my main file), things work perfectly, no error is thrown, and the program ends normally. Although it works, I still didn't get to the root cause, so can someone tell me what happened?

code
  • 5,690
  • 4
  • 17
  • 39
  • I haven't tried this, but using a package like this can help you detect what's keeping the process running: https://www.npmjs.com/package/why-is-node-still-running – Evert Dec 03 '21 at 06:06
  • Thanks, but the logs weren't really helpful (I couldn't understand it...). – code Dec 03 '21 at 18:46
  • Consider editing your question and adding the logs. Me or others might understand them! – Evert Dec 03 '21 at 20:00
  • @Evert just did. – code Dec 03 '21 at 20:20
  • Hey @code, that's pretty interesting!. It looks like you actually still have a running query by the time you want to shut down. (`SELECT * FROM `registration`). Any thoughts why that might still be running? – Evert Dec 03 '21 at 22:39
  • @Evert Could it be the promise functionality? – code Dec 03 '21 at 23:19
  • possible, have you considered using mysql2/promise instead? I wrote this article: https://evertpot.com/executing-a-mysql-query-in-nodejs/ which may help! – Evert Dec 04 '21 at 01:11

0 Answers0