Skip to content

Node process can't finish correctly after creation of Connection Pool #1917

Open
@komanton

Description

@komanton

I noticed that with some configuration of Connection Pool the Node process can't stop working and just hang.
In my case, it appears in mocha tests when mocha process can't finish after all tests.
This is simple project to reproduce the situation:

package.json

{
  "name": "mysql2-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "dependencies": {
    "mocha": "^10.2.0",
    "mysql2": "^3.2.0"
  },
  "author": "",
  "license": "ISC"
}

/test/test.js

const mysql = require('mysql2');

describe('test connection pool', function () {

  const pool = mysql.createPool({
    host: 'localhost',
    port: 3007,
    user: 'dev',
    password: 'dev',
    database: 'dev',
    multipleStatements: true,
    connectionLimit: 16,
    maxIdle: 0,
    idleTimeout: 60000,
    enableKeepAlive: true,
  });

  it('test stub', function () {
    // just place holder
  });

  this.afterAll(function () {
    console.log('Clean resources after all tests :');
  
    pool?.end();
  });  
});

Steps to reproduce:

  1. Run command npm test.
  2. All tests should be passed.

Actual behaviour: node process (i.e. mocha) is hang and can't finish.
Expected behaviour: node process (i.e. mocha) should finish correctly.

Note 1: Actually, I think this is very important issue because it may affect not only mocha process but also some processes in Docker environment and Lambda where processes should quickly start and finish to satisfy auto-scalabity algorithms.
Note 2: According to source code of mysql2, I noticed that in case of maxIdle < connectionLimit, mysql2 call clearTimeout. But looks like, it was forgotten to call clearTimeout on pool.end(). That's why 'event loop' is not empty, and it prevents a process from finishing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions