porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.03k stars 255 forks source link

Possible issue with cloudflare workers caused by a canceled promise #865

Closed unlocomqx closed 1 month ago

unlocomqx commented 1 month ago

Edit: I found the solution ✅. I have to recreate the db client with each request.

Hi 👋

I'm not entirely sure of the exact cause of this issue but I created a minimal repro. maybe someone can pinpoint the source of the issue.

Here's the repo https://github.com/unlocomqx/cloudflare-error-1101

Here's the deploy to see the issue directly (The problem occurs once every two requests) https://b8aef2ad.cloudflare-error-1101.pages.dev/db

CleanShot 2024-05-14 at 01 48 36@2x

The repo also has instructions to see the issue locally CleanShot 2024-05-14 at 01 48 11@2x

Here's the error from wrangler

Error ✘ [ERROR] A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished. ✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
unlocomqx commented 1 month ago

A more minimal repro with a better error message https://github.com/unlocomqx/cloudflare-svelte-bug

✘ [ERROR] Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: WritableStreamSink)

      at EventEmitter.write
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/polyfills.js:182:16)
      at nextWrite
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/src/connection.js:252:22)
      at null.<anonymous>
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/polyfills.js:224:7)
      at new Query
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/src/query.js:35:9)
      at sql2
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/src/index.js:113:11)
      at load
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/.svelte-kit/output/server/entries/pages/_page.server.js:7:23)
      at load_server_data
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/.svelte-kit/output/server/index.js:1008:42)
      at null.<anonymous>
  (file:///Users/USER/code/tests/cloudflare/my-svelte-app/.svelte-kit/output/server/index.js:2538:24)
  {
    query: 'SELECT * FROM users',
    parameters: [],
    args: [],
    types: []
  }

Deployed

unlocomqx commented 1 month ago

I found the solution ✅

https://github.com/pocketbase/pocketbase/discussions/2185#discussioncomment-5549838

I have to recreate the db client with each new request!