Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CONNECTION_CLOSED on subscribe() #757

Open
dvv opened this issue Dec 13, 2023 · 3 comments
Open

CONNECTION_CLOSED on subscribe() #757

dvv opened this issue Dec 13, 2023 · 3 comments

Comments

@dvv
Copy link

dvv commented Dec 13, 2023

I use standard

const { unsubscribe: _unsubscribe } = await sql.subscribe(
  "*",
  (row, info) => {
    console.log("SUB", row, info)
  },
  () => {
    console.log("SUB!")
  },
)

and it works well.

When postgres goes down I immediately get

Unexpected error during logical streaming - reconnecting Error: write CONNECTION_CLOSED ...
    at closed (https://deno.land/x/postgresjs@v3.4.2/src/connection.js:443:57)
    at https://deno.land/x/postgresjs@v3.4.2/polyfills.js:138:30
    at Array.forEach (<anonymous>)
    at call (https://deno.land/x/postgresjs@v3.4.2/polyfills.js:138:16)
    at closed (https://deno.land/x/postgresjs@v3.4.2/polyfills.js:127:5)
    at success (https://deno.land/x/postgresjs@v3.4.2/polyfills.js:109:7)
    at eventLoopTick (ext:core/01_core.js:189:11) {
  code: "CONNECTION_CLOSED",
  errno: "CONNECTION_CLOSED",

When postgres goes up then I get everything working except for the above subscription -- it never reconnects.

@Louis-Tian
Copy link

Just got caught with the same error. And it's causing my database to bloat with the WAL piling up...

@Louis-Tian
Copy link

Louis-Tian commented Jan 19, 2024

I believe the key problem here is the "subscribe" creates a temporary replication slot on the database. As soon as the database shuts down, that original replication slot is gone forever. Postgres.js at the moment is not smart enough to recreating a new replication slot when the database is back online.

@porsager What is really problematic at the moment is that there is no way for us to handle it ourselves in this situation. As the error is being completely swallowed by the stream here.

postgres/src/subscribe.js

Lines 99 to 107 in 6f20a48

stream.on('data', data)
stream.on('error', error)
stream.on('close', sql.close)
return { stream, state: xs.state }
function error(e) {
console.error('Unexpected error during logical streaming - reconnecting', e)
}

I think even just rethrow the error would be better. Because then I can catch that error using the global "UncaughtException" event. And even if I don't, the program would completely crash and restarted by the container runtime or process manager alike.

@dvv
Copy link
Author

dvv commented Jan 19, 2024

@Louis-Tian right.
I also drafted master...dvv:postgres:master to move "close" logic uplevel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants