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
update - did a fresh project and still getting same issue.
@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?

Hi
This usually happens when you try to aaccess same context in 2 differnt places. It's not better auth specific problem
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

when i deploy without kyselyAdapter - just running in memory, it works fine.
first type should be "postgres"
it might have something to do with how env is resovled in cf workers
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?
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?



Also, confirm it's not the env variables access. Error still happens even if I hard code it in.
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
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.
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.

Update: not a good, but still a solution is to wrap betterAuth around in a function and call that.


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.
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.
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.
Yeah the V8 isolate environment is pain in the ass to deal with but its so worth it