Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
komanton opened this issue Mar 27, 2023 · 5 comments
Open
Labels

Comments

@komanton
Copy link

komanton commented Mar 27, 2023

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.

@sidorares sidorares added the bug label Mar 28, 2023
@sidorares
Copy link
Owner

Thanks for your research. Can confirm this issue, clearing idle timer on pool end seems to solve it. I'll work on a fix & unit test

@jeremysf
Copy link

I'm running into this issue also when running tests with maxIdle < connectionLimit.

Any suggested workarounds to more cleanly shutdown?

@anton-fjg
Copy link

anton-fjg commented Jul 28, 2023

@jeremysf As a work around, you can use mocha's --exit flag.

@songpr
Copy link

songpr commented Aug 3, 2023

I'm running into this issue also when running tests with maxIdle < connectionLimit.

Any suggested workarounds to more cleanly shutdown?

I've this issue too, I workaround this by just remove maxIdle

@dennys
Copy link

dennys commented Sep 13, 2023

I also have this issue, modify maxIdle doesn't work for me, but it works to add --exit in Mocha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants