Skip to content

CONNECTION_CLOSED on subscribe() #757

Closed

Description

@dvv

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/[email protected]/src/connection.js:443:57)
    at https://deno.land/x/[email protected]/polyfills.js:138:30
    at Array.forEach (<anonymous>)
    at call (https://deno.land/x/[email protected]/polyfills.js:138:16)
    at closed (https://deno.land/x/[email protected]/polyfills.js:127:5)
    at success (https://deno.land/x/[email protected]/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.

Activity

  1. Louis-Tian commented on Jan 10, 2024

    @Louis-Tian

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

  2. Louis-Tian commented on Jan 19, 2024

    @Louis-Tian

    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.

  3. dvv commented on Jan 19, 2024

    @dvv
    Author

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

  4. smartcrash commented on Oct 11, 2024

    @smartcrash

    I having this same issue. Have you found a work around for this?

  5. X-43A commented on Nov 7, 2024

    @X-43A

    Same issue. A solution would be appreciated. Thanks

  6. hanszoons commented on Dec 20, 2024

    @hanszoons

    Ran into the same issue. The database connection itself gets reinstated automatically, but the subscription is never recreated without a full client restart.

  7. pmielle commented on Jul 27, 2025

    @pmielle

    hey, same issue here. Any solutions/workarounds? Thanks!

  8. IAkumaI commented on Jul 13, 2026

    @IAkumaI

    Any progress on this?

  9. porsager commented on Oct 29, 2026

    @porsager
    Owner

    In v4 sql.subscribe reconnects with a backoff after the connection closes, and the error goes to the onerror callback (the fourth argument) instead of an unhandled CONNECTION_CLOSED; without one it's logged. onsubscribe is called again after each reconnect.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions