cloudflare error

Hello - I have a sveltekit application which is hosted on cloudflare pages. I am using google social login, and it works fine in development. but in prod, it gives me 500 error right after I click on google account cookies are not set, and I get this in my cloudflare log: "message": [ "# SERVER_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: Writable)" ] What might be happening? How can I get this working?
19 Replies
dimsumham
dimsumhamOP2w ago
update - did a fresh project and still getting same issue.
dimsumham
dimsumhamOP2w ago
@bekacru for what it's worth - been trying my best to get to the bottom of the issue, and says this is it? does it make any sense?
No description
dimsumham
dimsumhamOP7d ago
Hi
bekacru
bekacru7d ago
This usually happens when you try to aaccess same context in 2 differnt places. It's not better auth specific problem
dimsumham
dimsumhamOP7d ago
sorry i just see you typing - got an update so it seems like the issue has to do with kyselyAdapter this is my auth.ts set up
dimsumham
dimsumhamOP7d ago
No description
dimsumham
dimsumhamOP7d ago
when i deploy without kyselyAdapter - just running in memory, it works fine.
bekacru
bekacru7d ago
first type should be "postgres" it might have something to do with how env is resovled in cf workers
dimsumham
dimsumhamOP7d ago
if it's env resolving shouldn't it still throw an error without kyselyAdapter since the GOOGLE_CLIENT_ID / SECRET resolves in the same call?
dimsumham
dimsumhamOP7d ago
Update - not just kyselyadapter. Seems to be happening with any sort of database writes, even with the most vanilla code. this is my set up - still having same issues. Every time, at callback URL stage, it errors out. Either with I/O error, or "this code will never generate a response" error. One other thing to mention is that I'm using an auth database that's sitting on a vps but not sure if that matters?
No description
No description
No description
dimsumham
dimsumhamOP7d ago
Also, confirm it's not the env variables access. Error still happens even if I hard code it in.
bekacru
bekacru7d ago
Im not sure what you can do to fix it but it seems like it's caused by the db connection being used outside of the handler context
dimsumham
dimsumhamOP7d ago
Is there a recommended set up for cloudflare/serverless deployment where both client and server auth sits with the serverless provider, and database is remote? Or any other combo of solutions to get around this If it helps at all - when I literally just enter the callback url again into the address bar it works.
dimsumham
dimsumhamOP7d ago
Update: cloned better-auth repo, added some logs, and running it locally using wrangler pages dev. Seems like this is the issue. will continue investigating and report back.
No description
dimsumham
dimsumhamOP6d ago
Update: not a good, but still a solution is to wrap betterAuth around in a function and call that.
No description
No description
stu
stu3d ago
Hey @dimsumham I'm getting the same error. Did you find any clean way to resolve, or should I wrap auth in a function? I'm also using postgres.
dimsumham
dimsumhamOP3d ago
Wrapping in a function is the cleanest and easiest way to resolve this issue. If you're worried about performance / overwhelming auth server then you could use hyperdrive or a pooler on database side.
stu
stu3d ago
Nice, I'm new to Cloudflare, so just trying to get things working. I'll look into Hyperdrive, sounds like a good fit for using Postgres with Cloudflare.
dimsumham
dimsumhamOP3d ago
Yeah the V8 isolate environment is pain in the ass to deal with but its so worth it

Did you find this page helpful?