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.

client in pool timed out, causing pool to fail for all future queries

See original GitHub issue

Hello! I’m using a pg-pool in a lambda environment. Last night, the pool hit an error:

2020-06-19T17:28:50.154-04:00 | START
2020-06-19T17:28:50.257-04:00 | ERROR	Invoke Error
"Error: Connection terminated due to connection timeout",
        "    at Timeout.<anonymous> (/var/task/node_modules/pg/lib/client.js:103:26)",

My pool is a static global to allow lambda to reuse across invocations. I guess pg wasn’t smart enough to check idle timeout before assigning a client, and in this case it was already dead – is there a way to flush the pool on each lambda invocation startup?

Anyway, somehow this first timeout put the pool in a bad state, and every call to the pool (which are made using the .query helper) instantly failed with a timeout.

  | 2020-06-19T17:29:17.492-04:00 | START
  | 2020-06-19T17:29:17.496-04:00 | ERROR	Invoke Error
Error: Connection terminated due to connection timeout

My config (pg 8.0.3, xray 3.0.1)

import pg from 'pg';
import xrayPostgres from 'aws-xray-sdk-postgres';
const { Pool } = xrayPostgres(pg)
const pool = new Pool({
      connectionTimeoutMillis: 5000,
      idleTimeoutMillis: 60000,
      ssl: ...
});

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sehropecommented, Jun 20, 2020

The issue is that when the lambda freezes and then thaws, any TCP connections are broken but not necessarily notified that they’re broken. This can lead to all sorts of issues so the simplest advice is not to use any long lived pooling in lambda and keep things stateless.

See this thread for more on this topic: https://github.com/brianc/node-postgres/issues/2112

There’s likely some improvements that could be done to the pool / client code to better handle these types of errors but it’s not going to be anything that will make those connections usable. At best it will end up attempting to use them, hanging or erroring out, and then getting you a new connection after some timeout.

If you still want to stick with the pool interface to handle the connection management (as it’s convenient for cleaning up resources), check out the “ZeroPool” mentioned here: https://github.com/brianc/node-postgres/issues/1938

1reaction
sehropecommented, Jul 2, 2020

That’s probably a bad idea anyway (each IDLE connections take up some resources so you can’t have infinite of them). Even if you could, there’s no way to guarantee it will survive a freeze / thaw. Better to avoid it entirely.

If you’re concerned about slow connection times then consider adding a dedicated connection pooler like pgbouncer and have your Lambda target the pooler.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I solve a connection pool problem between ASP.NET ...
System.InvalidOperationException: 'Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all ...
Read more >
Common Reasons Why Connections Stay Open for a Long ...
When you are monitoring connection pool performance, you may occasionally notice that connections are staying open for long periods of time.
Read more >
Requesting client from pool timed out - SAP Community
where({ID:ID});. When I try to perform any query, I am getting the following error: Requesting client from pool timed out. Max pool size...
Read more >
Apache Tomcat 8 (8.5.84) - The Tomcat JDBC Connection Pool
(int) The timeout in seconds before a connection validation queries fail. This works by calling java.sql.Statement.setQueryTimeout(seconds) on ...
Read more >
Pool Configuration Example and Settings - VMware Docs
Attribute Name Description Default max‑connections ‑1 name Pool name.
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