porsager / porsager/postgres

Never-ending CONNECTION_CLOSED errors

Open
#921 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

I'm putting this here since the docs state: This should not happen during normal operation, so please create an issue if this was unexpected.

So, my node projects makes about one database query call every 10 minutes, and every now and then, I get errors like these:
image

The error in text-form:

Error during getGlobalLeaderboards: Error: write CONNECTION_CLOSED <host>:<port>
    at TLSSocket.closed (/home/container/node_modules/postgres/cjs/src/connection.js:438:57)
    at TLSSocket.emit (node:events:529:35)
    at node:net:350:12
    at Socket.done (node:_tls_wrap:657:7)
    at Object.onceWrapper (node:events:632:26)
    at Socket.emit (node:events:517:28)
    at TCP.<anonymous> (node:net:350:12)
    at cachedError (/home/container/node_modules/postgres/cjs/src/query.js:170:23)
    at new Query (/home/container/node_modules/postgres/cjs/src/query.js:36:24)
    at sql (/home/container/node_modules/postgres/cjs/src/index.js:112:11)
    at getGlobalLeaderboards (/home/container/Discord Bots/resources/databases/Bomber Bot XP - DB.js:119:41) {
  code: 'CONNECTION_CLOSED',
  errno: 'CONNECTION_CLOSED',
  address: [
    '<host>'
  ],
  port: [ 5432 ]
}

The connection to my database is closing, how do I solve that from happening? Or is it possible to programmatically check if my connection is closed, and if so, re-open it, and run the query again?

Here's what my code looks like:

const postgres = require("postgres");

let { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env;

const sql = postgres({
    host: PGHOST,
    database: PGDATABASE,
    username: PGUSER,
    password: PGPASSWORD,
    port: 5432,
    ssl: "require",
    connection: {
        options: `project=${ENDPOINT_ID}`,
    },
    onnotice: false
});

const getBomber = async (id) => {
    try {
        if (!id) {
            console.log("Error during getBomber: No ID provided.");
            return;
        }
        return (await sql`SELECT * FROM bomber WHERE id = ${id}`)[0];
    } catch (error) {
        console.error('Error during getBomber:', error);
        return 'error';
    }
};

Also, I'm using a "pooler", could be apart of the issue -I don't really know the difference between a pool and client, iirc the pool is for many queries in one session or something? And since my bot is 24/7 it seemed appropiate, but I'm not fully sure.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the connection and query stack locations shown in cjs/src/connection.js, cjs/src/query.js, and cjs/src/index.js, then review how the supplied pooler behaves after the idle interval. Reproduce the CONNECTION_CLOSED error using the provided sql configuration and getBomber query. Done requires an agreed, verified connection-recovery behavior or clear documentation of the supported approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, postgres
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.