porsager / porsager/postgres

The connection reserved for LISTEN is not closed after the last subscribed-to channel is unsubscribed from

Open
#1,022 0 comments 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 noticed that the Node.js process does not come to an end after using the listening mechanism. That's because the connection reserved for LISTEN statements is not closed even after the last channel is unsubscribed from. And even if all other connections are closed via close().

Use this program:

const sql = require("postgres")(...);

sql.listen("topic", console.log.bind(null, "Received:"), async () => {
	await sql.unsafe(`SELECT pg_notify('topic', 'something')`);
}).then(({ unlisten }) => {
	setTimeout(async () => {
		await sql.close();
		await unlisten();
		// sql.unsafe(`SELECT 1`);
	}, 1000);
});

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 by running the JavaScript reproduction in the issue and inspect the listen/unlisten connection lifecycle. The fix is complete when closing the client and unsubscribing from the last channel allow the Node.js process to exit without another query keeping the connection open.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, postgresql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.