porsager / porsager/postgres

subscribe error on cloudflare: 'socket.resume is not a function'

Open
#975 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 setting up a simple subscription in my Durable Object as follows:

const { unsubscribe } = await sql.subscribe('*:file:id=' + fileId, (row, info) => {
  // ...
})

and it's failing with this error

✘ [ERROR] Unexpected error during logical streaming - reconnecting TypeError: socket.resume is not a function

    at Duplex.read [as _read] (node_modules/postgres/cf/src/connection.js:865:16)
    at Readable.read (node-internal:streams_readable:452:12)
    at resume_ (node-internal:streams_readable:881:12)
    at null.<anonymous> (node-internal:process:14:28)

which points to this line https://github.com/porsager/postgres/blob/a2c7de12b3bfc6809051d94ba6115150f80678e3/cf/src/connection.js#L864

Throwing the above error seems to send it into some kind of reconnection loop. If I log out the socket object I get

  EventEmitter {
   _events: [Object: null prototype] {
     error: [Function: error],
     close: [AsyncFunction: closed],
     drain: [Function: drain],
     connect: [Function: connected],
     data: [Function: data]
   },
   _eventsCount: 5,
   _maxListeners: undefined,
   readyState: 'open',
   raw: Socket {
     opened: Promise { [Object] },
     closed: Promise { <pending> },
     writable: WritableStream { locked: true, [state]: 'writable', [expectsBytes]: true },
     readable: ReadableStream {
       locked: true,
       [state]: 'readable',
       [supportsBYOB]: true,
       [length]: undefined
     }
   },
   writer: WritableStreamDefaultWriter {
     desiredSize: 1,
     ready: Promise { undefined },
     closed: Promise { <pending> }
   },
   reader: ReadableStreamDefaultReader { closed: Promise { <pending> } },
   connect: [AsyncFunction: connect],
   write: [Function: write],
   end: [Function: end],
   destroy: [Function: destroy],
   read: [AsyncFunction: read],
   ssl: false,
   host: '127.0.0.1',
   port: 6543,
   [Symbol(kCapture)]: false
 }

and looking at the cloudflare socket polyfill code it indeed seems like there is no 'pause'/'resume' implementations https://github.com/porsager/postgres/blob/a2c7de12b3bfc6809051d94ba6115150f80678e3/cf/polyfills.js#L132

I tried playing around with providing naive implementations of these methods but I couldn't make the subscription work.

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 reading the socket handling in cf/src/connection.js around line 864 and the Cloudflare socket polyfill in cf/polyfills.js around line 132. Reproduce the sql.subscribe call in a Durable Object and trace the stream behavior; done means subscriptions work without the socket.resume error or reconnection loop.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql
Domain
backend, cloud, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.