Skip to content

[BUG] postgres throws errors at runtime #548

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

Open
ka-raa-ge opened this issue Apr 8, 2025 · 8 comments
Open

[BUG] postgres throws errors at runtime #548

ka-raa-ge opened this issue Apr 8, 2025 · 8 comments
Labels
bug Something isn't working

Comments

@ka-raa-ge
Copy link

Describe the bug

Using the postgres-js driver to access a database works correctly on the first request, but fails with the following errors on every other subsequent request.

X [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.
[wrangler:inf] GET / 500 Internal Server Error (9ms)
X [ERROR] Uncaught (in response) Error: The script will never generate a response.
X [ERROR] Uncaught (async) Error: Promise will never complete.

Steps to reproduce

  1. Create an opennext project using npm create cloudflare@latest -- my-next-app --framework=next --platform=workers
  2. Install postgres npm install --save postgres
  3. Create a database script. Making sure to have the DATABASE_URL defined to a valid pg database:
import postgres from "postgres";
const connectionString = process.env.DATABASE_URL;
export const client = postgres(connectionString!);
  1. Replace the contents of app/page.tsx with the following. Change the db query to match a test table in your databse.
import { client } from "@/db/db";

export const dynamic = "force-dynamic";

export default async function Home() {
  const result = await client`SELECT * FROM users;`;
  return <h1>{JSON.stringify(result)}</h1>;
}
  1. Run the worker npm run preview
  2. Open the page in the browser, refresh the page to trigger the error.

Expected behavior

The request should consistently resolve, fetching from the database and return the data.
An equivalent request on a bare-bones typsecript cloudflare worker project resolves the same request without issues.

@opennextjs/cloudflare version

~1.0.0-beta.0 || ^1.0.0

Wrangler version

^4.8.0

next info output

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024
  Available memory (MB): 31983
  Available CPU cores: 20
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.8.0
Relevant Packages:
  next: 15.2.4 // Latest available version is detected (15.2.4).
  eslint-config-next: 15.2.4
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

Additional context

No response

@ka-raa-ge ka-raa-ge added bug Something isn't working triage labels Apr 8, 2025
@vicb
Copy link
Contributor

vicb commented Apr 8, 2025

Could you attach a repro with a DB URL populated so that we can reproduce? (link to a GH repository)

You can also give Hyperdrive a try

@ka-raa-ge
Copy link
Author

ka-raa-ge commented Apr 8, 2025

Could you attach a repro with a DB URL populated so that we can reproduce? (link to a GH repository)

@vicb I've created a private repo, so I don't publicly leak my db password. I've sent you the invite

@mhart
Copy link

mhart commented Apr 10, 2025

I ran into the same problem.

(you can create a DB for free at Neon and reproduce this)

Same code works fine in a normal Worker – just not when compiled with opennextjs-cloudflare build

@ka-raa-ge
Copy link
Author

@mhart my current workaround is to use neon's serverless driver. But I don't believe that it's universal. It didn't work with supabase's postgres db. So the vendor- lock is not ideal.
Another option is to have all the database code on a separate worker and access it with your opennext worker through RPC. But that adds complexity when running local dev builds.

@vicb using hyperdrive on top of the postgres-js driver did not fix the issue. The invite is still pending, let me know if you're having any issues with accessing the reproduction repo.

@mhart
Copy link

mhart commented Apr 10, 2025

I suspect the issue is here: https://github.com/porsager/postgres/blob/089214e85c23c90cf142d47fb30bd03f42874984/package.json#L11

Basically this won't get included correctly under an OpenNext build because the esbuild platform is set to node

However, under a normal Worker build, wrangler will set the workerd esbuild condition, and so this file will be used

@MingkaiChen
Copy link

We experienced the same issue. After investigating, I believe the reason @mhart mentioned is correct.

@Msekoo
Copy link

Msekoo commented Apr 22, 2025

✘ [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.

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.

[wrangler:inf] POST /en/templates 500 Internal Server Error (42ms)
✘ [ERROR] Uncaught (async) Error: Promise will never complete.

"postgres": "^3.4.4",
"wrangler": "^4.12.0",
"drizzle-kit": "^0.26.2",
"drizzle-orm": "^0.31.4",
"drizzle-zod": "^0.5.1",
"@opennextjs/cloudflare": "^1.0.0-beta.3",

This happens occasionally. A page may be normal when opened first, but these errors will appear when opened again, and normal and abnormal situations appear alternately. I suspect it is related to the database? I use hyperdrive and my project is nextjs

@conico974
Copy link
Collaborator

@Msekoo @mhart @MingkaiChen @ka-raa-ge Could you guys try with this PR ? #593 (comment)
Don't forget to add postgres to serverExternalPackages of Next config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

6 participants