Async operations don't work inside databaseHooks on Cloudflare Workers #1284

Closed
opened 2026-03-13 08:31:17 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @ahudovich on GitHub (May 30, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I want to send Discord notification when a new user is created.

But on Cloudflare when doing any type of async operation inside databaseHooks it throws this I/O error:

"# 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)"

I tried a lot of options - standard $fetch request (I use Nuxt), tried to use Cloudflare Queue inside the hook, removing await...

But it still throws this issue.

import { betterAuth } from "better-auth"

export const auth = betterAuth({
  databaseHooks: {
    user: {
      create: {
        after: async (user) => {
          const message = `New user registered: ${user.email}`

          $fetch(process.env.DISCORD_WEBHOOK_URL, {
            method: 'POST',
            body: {
              content: message,
            },
          })
        }
      },
    },
  },
})

I understand why it's happening, is there any workarounds for now and plans to make it work on Cloudflare?

Current vs. Expected behavior

When making a request inside databaseHooks on Cloudflare it works.

What version of Better Auth are you using?

1.2.7

Provide environment information

Cloudflare Workers

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @ahudovich on GitHub (May 30, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I want to send Discord notification when a new user is created. But on Cloudflare when doing any type of async operation inside `databaseHooks` it throws this I/O error: "# 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)" I tried a lot of options - standard $fetch request (I use Nuxt), tried to use Cloudflare Queue inside the hook, removing await... But it still throws this issue. ``` import { betterAuth } from "better-auth" export const auth = betterAuth({ databaseHooks: { user: { create: { after: async (user) => { const message = `New user registered: ${user.email}` $fetch(process.env.DISCORD_WEBHOOK_URL, { method: 'POST', body: { content: message, }, }) } }, }, }, }) ``` I understand why it's happening, is there any workarounds for now and plans to make it work on Cloudflare? ### Current vs. Expected behavior When making a request inside databaseHooks on Cloudflare it works. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash Cloudflare Workers ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
Author
Owner

@thomas-desmond commented on GitHub (Jun 25, 2025):

Hi, unfortunately I do not have a solution for you but I am running into a similar issue with Better-Auth no Cloudflare Workers. My error is the same except for I/O type: Native.

"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: Native)"

I'm getting inconsistent failure where about 50% of my requests work but it's very intermittent and seemingly random.

You said: "I understand why it's happening"

Can you explain more of why you think your issue is happening.

@thomas-desmond commented on GitHub (Jun 25, 2025): Hi, unfortunately I do not have a solution for you but I am running into a similar issue with Better-Auth no Cloudflare Workers. My error is the same except for I/O type: Native. "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: Native)" I'm getting inconsistent failure where about 50% of my requests work but it's very intermittent and seemingly random. You said: "I understand why it's happening" Can you explain more of why you think your issue is happening.
Author
Owner

@mw10013 commented on GitHub (Aug 9, 2025):

@AndreiHudovich Have you tried creating the auth object within the worker fetch handler? I wonder if creating the auth object at the module level is problematic since there is no request context.

@mw10013 commented on GitHub (Aug 9, 2025): @AndreiHudovich Have you tried creating the auth object within the worker `fetch` handler? I wonder if creating the auth object at the module level is problematic since there is no request context.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1284