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.

Connection terminated unexpectedly (Postgresql / AWS Lambda)

See original GitHub issue

Environment

Knex version: 0.20.8 Database + version: Postgres 9.5.14 OS: AWS Lambda - Node 12.x

Recently, I’ve been seeing upticks in a connection terminated unexpectedly error. I can’t find a discernible pattern at this point but unlike #3523, it doesn’t seem to occur after long periods of activity. I see instances that are minutes apart in some cases.

I added some logging to the Knex afterCreate method and am seeing two errors:

  1. The connection terminated unexpectedly error
  2. And Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed - this seems to happen on the 1st or second invocation after seeing the connection terminated unexpectedly error.

I’ve tried a number of configurations with Knex pooling:

  1. { pool: { min: 1, max: 1 } }
  2. { pool: { min: 0 }
  3. { pool: { min: 1 } }

I’ve also tried setting { pool: { idleTimeoutMillis: 300000 } } as per #3523.

Additional information:

  • I’m defining my Knex instance outside of my AWS handlers, so the pool should be reused per container.
  • I’m using the following for my afterCreate function – I’m not sure if there’s a better way to get at the underlying error?
function afterCreate(connection, done) {
  connection.on('error', (err) => {
    console.log('POSTGRES CONNECTION ERROR');
    console.error(err)
  });
  done()
}

Appreciate any help! This is driving me nuts 😃

UPDATE: the only change that I’ve made is migrating the lambda function to Node 12 from Node 8 as AWS is removing support for 8. Unfortunately, it doesn’t look like I can go back. My other functions in production are on Node 8 and not seeing any issues 🤔🤔🤔

ISSUE FOUND: for whatever reason, reverting back to Node 8.x fixes the issue. Still not certain on the underlying cause.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:71 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
lastmjscommented, Sep 2, 2020

After digging into this more, it seems not possible to use prepared statements with any kind of transaction pooling on postgres. The two main types of pooling are session pooling and transaction pooling. Transaction pooling seems the most generally performant. Transaction pooling is what RDS Proxy uses. node-postgres or pg does not seem compatible with any kind of transaction pooling if using prepared statements, and Knex I believe always uses prepared statements. This discussion that I’ve started is probably better moved over to the pg repo. I’ll leave some links here:

3reactions
briandamagedcommented, Feb 27, 2020

@elhigu : when I said “query”, I meant “query a predicate method that returns synchronously”. I’m actually in the process of implementing an experimental branch in knex for this. If the experiment works, then the pg team can integrate the concept / expose a public API for it.

(Unfortunately, I’m encountering a minor roadblock at the moment. It appears that a handful of Postgres unit tests are attempting to connect to the wrong port. I’m not sure why this fix would suddenly uncover that issue)

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - "connection terminated unexpectedly" error with ...
I have a number of Node functions running on AWS Lambda. These functions have been using the Node 8 runtime but ...
Read more >
Troubleshooting AWS Lambda + AWS Aurora Serverless ...
The Postgres logs in CloudWatch indicate that during some scaling events, the database abruptly disconnects all sessions and shuts down before ...
Read more >
Troubleshoot RDS for PostgreSQL error "FATAL - AWS
You might get this error because the application that connects to the RDS for PostgreSQL instance abruptly creates and drops connections.
Read more >
AWS Lambda to RDS connection error - Reddit
server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request · could ...
Read more >
Connection terminated unexpectedly for long queries-postgresql
Turns out it was just a Railway issue (the PostgreSQL host I was using). The same issue appears when using Render and other...
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