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.

Pooling: read ETIMEDOUT error after idle connection in Node 14

See original GitHub issue

This is one of my first issues on a node library so I’m going to do my best to explain and show my findings but if anymore information is needed please let me know. We recently upgraded to Node 14 as Node 12 was reaching end of life in azure services. Since then we have been getting random service crashes with the following error.

Error: read ETIMEDOUT
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20)
    at TLSWrap.callbackTrampoline (internal/async_hooks.js:126:14)

I’ve looked at various other issues that people have reported and have implemented the keepAlive option and idleTimeoutMillis based on some findings and it did help make it more stable, however, after long periods of inactivity the crashes still occur. For reference we have a micro-service architecture with some services barely ever used as they are for one off integrations etc etc and not part of the main app. Our services share a ‘database-connection’ package in a lerna repo that handles the pooling of the connections. The code is as follows:

const { Pool } = require('pg');

const {
  POSTGRES_HOST,
  POSTGRES_PORT,
  POSTGRES_DATABASE,
  POSTGRES_USER,
  POSTGRES_PASSWORD,
  POSTGRES_MAX_CONNECTIONS,
  POSTGRES_CONNECTION_IDLE_TIMEOUT,
  POSTGRES_APPLICATION_NAME,
} = process.env;

const poolConfig = {
  host: POSTGRES_HOST,
  port: POSTGRES_PORT,
  database: POSTGRES_DATABASE,
  user: POSTGRES_USER,
  password: POSTGRES_PASSWORD,
  ssl: true,
  keepAlive: true,
  idleTimeoutMillis: 3000,
};

if (
  POSTGRES_MAX_CONNECTIONS &&
  !isNaN(Number.parseInt(POSTGRES_MAX_CONNECTIONS, 10))
) {
  poolConfig.max = Number.parseInt(POSTGRES_MAX_CONNECTIONS, 10);
}

if (
  POSTGRES_CONNECTION_IDLE_TIMEOUT &&
  !isNaN(Number.parseInt(POSTGRES_CONNECTION_IDLE_TIMEOUT, 10))
) {
  poolConfig.idleTimeoutMillis = Number.parseInt(
    POSTGRES_CONNECTION_IDLE_TIMEOUT,
    10
  );
}

if (POSTGRES_APPLICATION_NAME) {
  poolConfig.application_name = POSTGRES_APPLICATION_NAME;
}

let poolInstance = null;

const initializePool = () => {
  poolInstance = new Pool(poolConfig);
};

const pool = () => poolInstance;

module.exports = {
  initializePool,
  pool,
};

We then initialize our server on startup like

databaseConnection.initializePool();
const databaseConnectionPool = databaseConnection.pool();
databaseConnectionPool
  .connect()
  .then(() => (parseBoolean(process.env.RUN_MIGRATIONS)
    ? databaseMigrationUtility.executeMigrations(
      databaseConnection,
      'migrations',
      '../migration-files',
    )
    : Promise.resolve()))
  .then(() => {
    server.listen(process.env.PORT || 5010);
    monitoring.trackEvent('Started Service API');
  })
  .catch((error) => {
    logger.error(error);
  });

Queries to the database are made following the single query pattern from the docs.

const databaseConnection = require('database-connection'); // This is the index file of the pool connection above

const getExampleDataFromDatabase = (id1, id2) => {
  const query = `
    SELECT id1
    FROM TABLE_1
    WHERE id1 = $1
    AND id2  != $2`;
  return databaseConnection
    .pool()
    .query(query, [id1, id2])
    .then((result) => result.rows.map((row) => row.id));

After long periods of inactivity the service will crash with the error above. Is this expected behavior? Should we be handing connections differently? Let me know if you need anymore information

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
charmandercommented, Jun 22, 2022

yes

1reaction
charmandercommented, Jun 23, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize and Mysql throw connect ETIMEDOUT error In ...
I have a nodejs server with express and sequelize. When I was in development enviroment I didn't have any problem then in production ......
Read more >
NodeJS with Keep-Alives and Connection Reuse -
This post will be an overview of how to implement connection reuse on Azure with Node.js. By default, Node.js doesn't reuse connections ......
Read more >
Sequelize and Mysql throw connect ETIMEDOUT error
I have a nodejs server with express and sequelize. When I was in development enviroment I didn't have any problem then in production ......
Read more >
15 Common Error Codes in Node.js and How to Fix Them
The ETIMEDOUT error is thrown by the Node.js runtime when a connection or HTTP request is not closed properly after some time.
Read more >
Connection Pools with MariaDB Connector/Node.js (Promise ...
idleTimeout. Sets idle time after which a pool connection is released. Value must be lower than wait_timeout. In seconds (0 means never release)....
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