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.

Knex is not clearing timeouts after they have become unnecessary

See original GitHub issue

Environment

Knex version: 0.20.10 Database + version: postgres 12.1 OS: Linux Node: 12.15.0 Jest: 25.1.0

Bug

Jest prints a warning about open handles when a test creates and rollbacks a transaction. When I comment out transacting code, the warning disappears. The connection to postgres database is closed in afterAll call.

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your
 tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

--detectOpenHandles flag does not detect anything.

Here is the reproduction code:

const Knex = require('knex');

const db = Knex({
  client: process.env.DB_TYPE || 'pg',
  connection: {
    host: process.env.DB_HOST || 'localhost',
    port: Number(process.env.DB_PORT) || 5432,
    user: process.env.DB_USER || 'postgres',
    password: process.env.DB_USER || 'postgres',
    database: process.env.DB_DATABASE || 'postgres',
  },
});

describe('test', () => {
  afterAll(async () => {
    await db.destroy();
  });

  it('transacts', async () => {
    const t = await db.transaction();

    await t.rollback();
  });
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
prk3commented, Mar 4, 2020

@briandamaged It makes sense now!

let timeoutId;

const timeoutPromise = new Promise((res, rej) => {
  timeoutId = setTimeout(() => rej(new KnexTimeoutException), 5000);
});

const queryPromise = this.query(conn, 'ROLLBACK', 2, error).then(result => {
  clearTimeout(timeoutId);
  return result;
});

return Promise.race([timeoutPromise, queryPromise]).catch(err => { 
  if (!(err instanceof KnexTimeoutError)) { 
    return Promise.reject(err); 
  } 
  this._rejecter(error); 
});

This modification would solve my problem. But what if timeout rejects first? In my code the query handler would be cleaned up by db.destory(), but in general case?

1reaction
briandamagedcommented, Mar 4, 2020

@elhigu : Makes sense. In that case, we might just need to cancel the setTimeout(..) call like @prk3 suggested.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Knex: Timeout acquiring a connection. The pool is probably ...
The solution is to set propagateCreateError to false thus enabling knex to automatically reconnect on create connection failure instead of ...
Read more >
Changelog - Knex.js
Fix timeout method #4324; SQLite: prevent dropForeign from being silently ignored #4376. Typings ... CLI: Print help only when there are no arguments...
Read more >
Building a RESTful API with Koa and Postgres - Michael Herman
The API itself should follow RESTful design principles, using the basic HTTP verbs: GET, POST, PUT, and DELETE.
Read more >
Timers | Node.js v19.3.0 Documentation
Because the timer functions are globals, there is no need to call ... When called, the active Timeout object will not require the...
Read more >
Bookshelf.js | Home
The issue here is that Knex, the database abstraction layer used by Bookshelf, uses connection pooling and thus keeps the database connection open....
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