question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pool.query fails after calling pool.end

See original GitHub issue

Sometimes, I need to close the old pool each 5 mins and create a new one. So I can’t make sure pool.end is called after all queries end. Since it is said

The end method takes an optional callback that you can use to know once all the connections have ended. The connections end gracefully, so all pending queries will still complete and the time to end the pool will vary.

in the doc, I think I can just call pool.end any time I want, but I will occasionally get an error:

Error: Pool is closed.

So, What does pending queries mean in the doc? Is a query which have been called pool.query a pending query?

For example,

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret',
  database        : 'my_db'
});

pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});
pool.end();  // will be called somewhere else, but will raise the same error here

I’ve found this commit 76de66e2c36a6da71dcd010b3afe0029e5eb68bb fixed pool.getConnection race conditions, but I don’t quite understand what are the race conditions here. In my opinion, after I have called pool.query, this query is a pending query no matter what phase the operation is under the hood such as ping.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
HQideacommented, Aug 18, 2017

example:

const mysql = require('mysql');

class Driver {
  constructor() {
    this.pool = null;
  }

  getPool() {
    if (!this.pool) {
      const pool = this.pool = mysql.createPool({
        host: '',
        port: 3306,
        user: '',
        password: '',
        database: ''
      });
      setTimeout(() => {
        this.pool = null;
        pool.end();
      }, 1000);
    }
    return this.pool;
  }

  query(cb) {
    this.getPool().query('show tables', cb);
  }
}

const driver = new Driver();

function call() {
  driver.query((err, data) => {
    if (err) {
      console.log(err);
      return;
    }
    setTimeout(() => {
      call();
    }, Math.random() * 1000);
  });
}
call();

error:

{ Error: Pool is closed.
    at /path/to/code/node_modules/_mysql@2.14.1@mysql/lib/Pool.js:149:17
    at Array.forEach (native)
    at Pool.releaseConnection (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/Pool.js:148:37)
    at Pool._removeConnection (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/Pool.js:279:8)
    at Pool._purgeConnection (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/Pool.js:260:8)
    at Ping.onOperationComplete [as _callback] (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/Pool.js:101:12)
    at Ping.Sequence.end (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/protocol/sequences/Sequence.js:88:24)
    at Ping.Sequence.OkPacket (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/protocol/sequences/Sequence.js:97:8)
    at Protocol._parsePacket (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/protocol/Parser.js:76:12)
    --------------------
    at Pool.query (/path/to/code/node_modules/_mysql@2.14.1@mysql/lib/Pool.js:199:23)
    at Driver.query (/path/to/code/test.js:26:20)
    at call (/path/to/code/test.js:33:10)
    at Timeout.setTimeout (/path/to/code/test.js:39:7)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5) code: 'POOL_CLOSED' }

It may not raise an error the first time you run, but it will eventually if you run several times.

1reaction
sidorarescommented, Aug 18, 2017

Thanks @HQidea , I can reproduce it.

So what’s happening is

  1. you do pool.query() first.
  2. pool is in the state when number of active connections already maxed so inside pool.query() it has to wait for idle connection
  3. pool.end() marks pool to a ‘closing’ state
  4. one of connections finishes previous query and becomes idle, pool.query() from 1) gets connection tries to do connectcion.query() and fails because pool is in closed state
Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL Can not open connection after call connection.end ...
The problem is the first time I try login worked fine, but if I try the same login API again throw the error...
Read more >
Connection Pools with MariaDB Connector/Node.js (Promise ...
Ends or closes the connection pool and the underlying connections after all running queries have completed. Returns a Promise object that resolves with...
Read more >
node-mssql | Microsoft SQL Server client for Node.js
In practice, once the pool is created then there will be no delay for the next connect() call. Also notice that we do...
Read more >
Node.js with MySQL - w3resource
It provides all most all connection/query from MySQL. ... If false, the pool will immediately call back with an error. (Default: true).
Read more >
Use Node.js to connect and query Azure Cosmos DB for ...
It's sometimes possible that database requests from your application fail. Such issues can happen under different scenarios, such as network ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found