Skip to content

Conversation

@igorlukanin
Copy link
Member

Add error handlers to pool clients to prevent unhandled error events from crashing the process when PostgreSQL connections are terminated unexpectedly (e.g., when max connections are reached).

Check List

  • Tests have been run in packages where changes have been made if available
  • Linter has been run for changed code
  • Tests for the changes have been added if not covered yet
  • Docs have been added / updated if required

Issue Reference this PR resolves

Fixes #10142

Conversation with Claude

claude.txt

Add error handlers to pool clients to prevent unhandled error events
from crashing the process when PostgreSQL connections are terminated
unexpectedly (e.g., when max connections are reached).

Fixes #10142
@igorlukanin igorlukanin requested a review from a team as a code owner December 22, 2025 16:15
@igorlukanin igorlukanin requested a review from Copilot December 22, 2025 16:15
@github-actions github-actions bot added driver:postgres Issues relating to the Postgres driver driver:questdb javascript Pull requests that update Javascript code data source driver labels Dec 22, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses unhandled error events from PostgreSQL connections that can crash the Node.js process when database connections are terminated unexpectedly (e.g., due to max connections being reached). The changes replace direct console.log calls with the proper databasePoolError method and add error handlers to individual pool clients.

  • Replaces console.log statements with calls to databasePoolError method in pool error handlers
  • Adds error event listeners to individual pool clients to prevent unhandled errors from crashing the process
  • Applies the same fix consistently across both PostgresDriver and QuestDriver

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/cubejs-postgres-driver/src/PostgresDriver.ts Updated pool error handler to use databasePoolError and added client error handlers in stream() and queryResponse() methods
packages/cubejs-questdb-driver/src/QuestDriver.ts Updated pool error handler to use databasePoolError and added client error handler in queryResponse() method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Attach error handler to the client to prevent unhandled error events
// from crashing the process when connections are terminated unexpectedly.
// See: https://github.com/brianc/node-postgres/issues/2112
conn.on('error', (err) => {
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error listener is being attached each time a connection is acquired from the pool, but it's never removed. When connections are reused from the pool, this will result in multiple identical error listeners accumulating on the same connection object, causing a memory leak. Consider using conn.once('error', ...) instead of conn.on('error', ...) to ensure the listener is automatically removed after the first error, or explicitly remove the listener in the finally block or release callback.

Suggested change
conn.on('error', (err) => {
conn.once('error', (err) => {

Copilot uses AI. Check for mistakes.
Comment on lines +355 to +357
conn.on('error', (err) => {
this.databasePoolError(err);
});
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error listener is being attached each time a connection is acquired from the pool, but it's never removed. When connections are reused from the pool, this will result in multiple identical error listeners accumulating on the same connection object, causing a memory leak. Consider using conn.once('error', ...) instead of conn.on('error', ...) to ensure the listener is automatically removed after the first error, or explicitly remove the listener in the finally block.

Copilot uses AI. Check for mistakes.
// Attach error handler to the client to prevent unhandled error events
// from crashing the process when connections are terminated unexpectedly.
// See: https://github.com/brianc/node-postgres/issues/2112
conn.on('error', (err) => {
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error listener is being attached each time a connection is acquired from the pool, but it's never removed. When connections are reused from the pool, this will result in multiple identical error listeners accumulating on the same connection object, causing a memory leak. Consider using conn.once('error', ...) instead of conn.on('error', ...) to ensure the listener is automatically removed after the first error, or explicitly remove the listener in the finally block.

Suggested change
conn.on('error', (err) => {
conn.once('error', (err) => {

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Dec 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.17%. Comparing base (df8161c) to head (003c146).
⚠️ Report is 1 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (df8161c) and HEAD (003c146). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (df8161c) HEAD (003c146)
cubesql 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #10277       +/-   ##
===========================================
- Coverage   83.27%   55.17%   -28.11%     
===========================================
  Files         248      221       -27     
  Lines       74448    17202    -57246     
  Branches        0     3521     +3521     
===========================================
- Hits        61999     9491    -52508     
+ Misses      12449     7216     -5233     
- Partials        0      495      +495     
Flag Coverage Δ
cube-backend 55.17% <ø> (?)
cubesql ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data source driver driver:postgres Issues relating to the Postgres driver driver:questdb javascript Pull requests that update Javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cube.js terminates when postgres connection is terminated

3 participants