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?
22 Replies
dimsumham
dimsumhamOP4w ago
update - did a fresh project and still getting same issue.
dimsumham
dimsumhamOP4w 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
dimsumhamOP4w ago
Hi
bekacru
bekacru4w ago
This usually happens when you try to aaccess same context in 2 differnt places. It's not better auth specific problem
dimsumham
dimsumhamOP4w 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
dimsumhamOP4w ago
No description
dimsumham
dimsumhamOP4w ago
when i deploy without kyselyAdapter - just running in memory, it works fine.
bekacru
bekacru4w ago
first type should be "postgres" it might have something to do with how env is resovled in cf workers
dimsumham
dimsumhamOP4w 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
dimsumhamOP4w 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
dimsumhamOP4w ago
Also, confirm it's not the env variables access. Error still happens even if I hard code it in.
bekacru
bekacru4w 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
dimsumhamOP4w 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
dimsumhamOP4w 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
dimsumhamOP4w 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
stu4w 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
dimsumhamOP4w 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
stu4w 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
dimsumhamOP4w ago
Yeah the V8 isolate environment is pain in the ass to deal with but its so worth it
stu
stu3w ago
hey @dimsumham, was curious if you had success using Cloudflare + Hyperdrive + better-auth? Basically, I have sign-in functionality server-side like:
const result = await auth(env).api.signInEmail({
body: {
email: email.toString(),
password: password.toString(),
},
asResponse: true,
});
const result = await auth(env).api.signInEmail({
body: {
email: email.toString(),
password: password.toString(),
},
asResponse: true,
});
But it's hanging forever when I hit it. No logs, just forever loading. Auth looks like:
export const auth = (env: Env) =>
betterAuth({
database: {
dialect: new PostgresJSDialect({
postgres: getDb(env), // Call getDb() as a function to create a new connection each time
}),
// dialect,
type: "postgres",
},
emailAndPassword: {
// email and password stuff
},
secret: BETTER_AUTH_SECRET,
});
export const auth = (env: Env) =>
betterAuth({
database: {
dialect: new PostgresJSDialect({
postgres: getDb(env), // Call getDb() as a function to create a new connection each time
}),
// dialect,
type: "postgres",
},
emailAndPassword: {
// email and password stuff
},
secret: BETTER_AUTH_SECRET,
});
And getDb function looks like:
export function getDb(env: Env) {
// Connection URL from environment variables
let connectionString = env.HYPERDRIVE.connectionString;

const sql = postgres(connectionString, {
max: 5,
// idle_timeout: 30,
ssl: import.meta.env.DEV,
// Debug options - remove in production
debug: true,
fetch_types: false,
});

return sql;
}
export function getDb(env: Env) {
// Connection URL from environment variables
let connectionString = env.HYPERDRIVE.connectionString;

const sql = postgres(connectionString, {
max: 5,
// idle_timeout: 30,
ssl: import.meta.env.DEV,
// Debug options - remove in production
debug: true,
fetch_types: false,
});

return sql;
}
Any ideas the changes I should make?
dimsumham
dimsumhamOP3w ago
Let me take a look tonight / tomorrow. I had an issue with this but did manage to get something working In your connection string - are you using any suffix? For schema etc
stu
stu3w ago
Amazing, thanks. Yes, the hyperdrive connection string has postgresql:// at the beginning. It's working in other instances, for example to fetch data. But it's getting stuck specifically with better-auth requests I think.

Did you find this page helpful?