node-pool icon indicating copy to clipboard operation
node-pool copied to clipboard

How to properly fail `factory.create` ?

Open silverbucket opened this issue 6 years ago • 4 comments

From the documentation it seems that you should not handle exceptions during .createPool() but instead during .aquire(). However if the factory.create() function fails, it continues to retry. I've tried both throwing an exception and rejecting the promise and I'm unable to get any controlled failure path from the operation.

const genericPool = require('generic-pool');
const pool = genericPool.createPool({
  create: () => {
    console.log('create called');
    return new Promise((resolve, reject) => {
      console.log('rejecting promise');
      throw new Error('connection problem');
      //reject('connection problem');
    });
  },
  destroy: () => {
    return new Promise((resolve, reject) => { resolve(); });
  }
}, { max: 2 });

console.log("acquire");
pool.acquire().then((client) => {
  console.log("success");
}, (err) => {
  console.log("controlled failure 1");
}).catch((err) => {
  console.log("controlled failure 2");
});

In the above example, aquire is never reached, and I get an endless loop of 'create called' and 'rejecting promise'. The behaviour is the same if I reject or throw.

...
create called
rejecting promise
create called
rejecting promise
create called
rejecting promise
create called
rejecting promise
^C

How can I reliably handle, for example, connection errors to a database, if the user provides incorrect config? Currently either I get a timeout unhandled exception from the DB library, or a hang/endless loop.

silverbucket avatar Sep 27 '19 13:09 silverbucket

Potential fix and workaround: #221

martin-randall-tanium avatar Dec 20 '21 17:12 martin-randall-tanium

@Kikobeats @sandfox any update on this issue? It continues to be a source of debugging trouble, with any connection error silently causing an endless loop.

silverbucket avatar Aug 21 '22 18:08 silverbucket

Can we use this to prevent the issue https://github.com/coopernurse/node-pool/issues/175#issuecomment-501826583

alanwu4321 avatar Oct 12 '22 04:10 alanwu4321