neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
318 stars 11 forks source link

Issues when connecting @neondatabase/serverless WebSocket Proxy with Cloudlfare Durable Objects #45

Closed TimoWilhelm closed 10 months ago

TimoWilhelm commented 10 months ago

I have an issue getting the WebSocket based Neon Client with the local WebSocket Proxy to connect in my Cloudflare Durable Object scripts.

Steps to reproduce

I've created a MRE Repository here: https://github.com/TimoWilhelm/mre-do-serverless-pg Make sure the local PG proxy is running (I've included a docker-compose file. You should be able to run it locally with wrangler by running npm install && npm run start.

The worker will expose two routes:

The /db route will instantiate a DB client and make a query to the database in a normal Cloudflare Worker fetch event handler. This works as expected.

For the /do route the DB client is instantiated using a state.blockConcurrencyWhile guard in the Durable Object constructor. This will lead to the following error when the request is made: Error: A call to blockConcurrencyWhile() in a Durable Object waited for too long. The call was canceled and the Durable Object was reset.

Environment

I'm using the WebSocket based Client since it's easier to run locally and better suited for the workload I'm planning to use. I've tested the HTTP fetch based neon() function and that is working as expected in the DO guard.

Logs

It seems like the inital connection is established but for some reason the client.connect() call never returns in the DO.

2023-08-27 22:20:40 2023/08/27 20:20:40 Got 58 bytes client->pg: AAAAOgADAAB1c2VyAHBvc3RncmVzAGRhdGFiYXNlAG1haW4AY2xpZW50X2VuY29kaW5nAFVURjgAAA==
2023-08-27 22:20:40 2023/08/27 20:20:40 Got 24 bytes pg->client: UgAAABcAAAAKU0NSQU0tU0hBLTI1NgAA
2023-08-27 22:21:10 2023/08/27 20:21:10 failed to read from socket: use of closed network connection
2023-08-27 22:21:10 2023/08/27 20:21:10 failed to handle websocket: websocket: close 1006 (abnormal closure): unexpected EOF
TimoWilhelm commented 10 months ago

After a bit more testing it seems like this error only happens when connecting to the local WebSocket Proxy. It seems to run fine when connecting to a remote Neon Database.

TimoWilhelm commented 10 months ago

This seems to be a more general issue with the workerd runtime and WebSockets. I've created an issue in the CF worker-sdk repo https://github.com/cloudflare/workerd/issues/1088