Description
Describe the bug
Cloudflare D1 SQL calls randomly causes silent errors with rendering
Steps to reproduce
https://github.com/tahmid-23/d1-bug
I tried to make a reproducible example. However there's a bit of a tradeoff between reproducibility and the bug occurring. On my complex website with a lot of images, animations, etc., this bug happens 90% of the time. In this test example, you have to refresh it on the order of 30 times for it to happen at least once. I'm not really sure why.
Essentially:
- run in npm dev
- layout.tsx exists
- page.tsx is a server component. it will call a server action
- the server action will connect to the D1 database, and run a trivial query ("SELECT 1" in my example)
- in the server component, render a client component
- the client component will console.log("Hello, World!") when it mounts via useEffect(() => {}, [])
Expected behavior
it should print out Hello, World! but it doesn't
@opennextjs/cloudflare version
1.0.4
Wrangler version
4.16.0
next info output
This project is configured to use pnpm because /home/tfz/d1-bug/package.json has a "packageManager" field
This project is configured to use pnpm because /home/tfz/d1-bug/package.json has a "packageManager" field
Operating System:
Platform: linux
Arch: x64
Version: #1 SMP PREEMPT_DYNAMIC Fri May 2 14:16:46 UTC 2025
Available memory (MB): 15668
Available CPU cores: 12
Binaries:
Node: 22.14.0
npm: 10.9.2
Yarn: N/A
pnpm: 10.9.0
Relevant Packages:
next: 15.3.2 // Latest available version is detected (15.3.2).
eslint-config-next: 15.3.2
react: 19.1.0
react-dom: 19.1.0
typescript: 5.8.3
Next.js Config:
output: N/A
Additional context
almost 90% of the time, if Hello, World! doesn't print, there will be an error printed to devtools console. It is usually something along the lines of unterminated comment, unterminated quote, etc. specifically in layout.js. This means that layout.js is truncated.
In my main codebase (not sent here), this issue happens if and only if I run an SQL query on the d1 database.
If I delay using setTimeout for 100ms right after the SQL query, this issue occurs less frequently. The longer I delay, the less likely this is to occur (1000ms seems to ensure it never occurs).
My robot friend who may be hallucinating is under the belief that d1 needs to close some resources when it finishes an SQL call, and if you stream and complete the response before it closes its resources, it will cause some issue. This would lead to the layout.js truncation. (just some idea that sounds plausible to me, I don't know if relevant)
I am not seeing any other relevant logs being printed out.