Error 500 when on parallel requests that execute get-session. #2402

Closed
opened 2026-03-13 09:50:35 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @xorweak on GitHub (Nov 26, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a better-auth server api (not use "monorepo", standalone API, I use hono)
  2. Consume multiple times get-session from another API (Server side, eg. "backend" better-auth client).
  3. You will get error 500 internal server error.

I notice that this error is caused by the native rateLimiter, taking down the whole better-auth api server when hit.

ps. sorry for the bad issue, I don't have too much time but this is a simple bug that can be easily reproducible with any better-auth conf.

Current vs. Expected behavior

Following previous steps, I expect that the api does not crash on mass requests traffic, but instead in somepoint It breaks and start returning error 500.

What version of Better Auth are you using?

1.4.0-beta.20

System info

-

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @xorweak on GitHub (Nov 26, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a better-auth server api (not use "monorepo", standalone API, I use hono) 2. Consume multiple times get-session from another API (Server side, eg. "backend" better-auth client). 3. You will get error 500 internal server error. I notice that this error is caused by the native rateLimiter, taking down the whole better-auth api server when hit. ps. sorry for the bad issue, I don't have too much time but this is a simple bug that can be easily reproducible with any better-auth conf. ### Current vs. Expected behavior Following previous steps, I expect that the api does not crash on mass requests traffic, but instead in somepoint It breaks and start returning error 500. ### What version of Better Auth are you using? 1.4.0-beta.20 ### System info ```bash - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the honobug labels 2026-03-13 09:50:35 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 26, 2025):

tag @better-auth-agent if you would like an answer from the Agent

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Nov 26, 2025): tag @better-auth-agent if you would like an answer from the Agent <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@dosubot[bot] commented on GitHub (Nov 26, 2025):

Hi @soknifedev! I'm Dosu and I’m helping the better-auth team.

This is a known issue when making many parallel get-session requests: the native rateLimiter itself returns a 429 error when the limit is hit, but under heavy parallel load, storage or concurrency issues (especially with the rateLimiter's backend or session storage) can cause unhandled exceptions that escalate to a 500 error. This is more likely if the default in-memory or database storage can't handle the concurrency, or if the rateLimiter and getSession both try to update the same keys at once. The server isn't supposed to crash, but backend failures can surface as 500 errors in getSession because all errors are caught and rethrown as INTERNAL_SERVER_ERRORs in the endpoint handler [source].

To mitigate this: you can explicitly disable rate limiting in development or tests by setting rateLimit: { enabled: false } in your config, or use a more robust custom storage backend for rate limiting if you expect high concurrency [source]. If you use API keys, make sure you aren't double-counting requests (see enableSessionForAPIKeys: true). For production, consider limiting parallelism on the client, adding retry logic, or using a queue/throttle to avoid flooding the server [source].

If you need to debug further, check your logs for the actual stack trace of the 500 error—this will help pinpoint if it's a storage backend failure, a race condition, or something else.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Nov 26, 2025): <!-- Greeting --> Hi @soknifedev! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known issue when making many parallel get-session requests: the native rateLimiter itself returns a 429 error when the limit is hit, but under heavy parallel load, storage or concurrency issues (especially with the rateLimiter's backend or session storage) can cause unhandled exceptions that escalate to a 500 error. This is more likely if the default in-memory or database storage can't handle the concurrency, or if the rateLimiter and getSession both try to update the same keys at once. The server isn't supposed to crash, but backend failures can surface as 500 errors in getSession because all errors are caught and rethrown as INTERNAL_SERVER_ERRORs in the endpoint handler [[source]](https://github.com/better-auth/better-auth/blob/e75d272004295e55a901519d605d72f84199e963/packages/better-auth/src/api/routes/session.ts#L29-L445). To mitigate this: you can explicitly disable rate limiting in development or tests by setting `rateLimit: { enabled: false }` in your config, or use a more robust custom storage backend for rate limiting if you expect high concurrency [[source]](https://github.com/better-auth/better-auth/issues/5070). If you use API keys, make sure you aren't double-counting requests (see `enableSessionForAPIKeys: true`). For production, consider limiting parallelism on the client, adding retry logic, or using a queue/throttle to avoid flooding the server [[source]](https://github.com/better-auth/better-auth/issues/4609). If you need to debug further, check your logs for the actual stack trace of the 500 error—this will help pinpoint if it's a storage backend failure, a race condition, or something else. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/8aa3e02b-7723-4a97-a4cc-e86a9f105f00?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20known%20issue%20when%20making%20many%20parallel%20get-session%20requests%3A%20the%20native%20rateLimiter%20itself%20returns%20a%20429%20error%20when%20the%20limit%20is%20hit%2C%20but%20under%20heavy%20parallel%20load%2C%20storage%20or%20concurrency%20issues%20%28especially%20with%20the%20rateLimiter%27s%20backend%20or%20session%20storage%29%20can%20cause%20unhandled%20exceptions%20that%20escalate%20to%20a%20500%20error.%20This%20is%20more%20likely%20if%20the%20default%20in-memory%20or%20database%20storage%20can%27t%20handle%20the%20concurrency%2C%20or%20if%20the%20rateLimiter%20and%20getSession%20both%20try%20to%20update%20the%20same%20keys%20at%20once.%20The%20server%20isn%27t%20supposed%20to%20crash%2C%20but%20backend%20failures%20can%20surface%20as%20500%20errors%20in%20getSession%20because%20all%20errors%20are%20caught%20and%20rethrown%20as%20INTERNAL_SERVER_ERRORs%20in%20the%20endpoint%20handler%20%5B%5Bsource%5D%5D%28https%3A//github.com/better-auth/better-auth/blob/e75d272004295e55a901519d605d72f84199e963/packages/better-auth/src/api/routes/session.ts%23L29-L445%29.%0A%0ATo%20mitigate%20this%3A%20you%20can%20explicitly%20disable%20rate%20limiting%20in%20development%20or%20tests%20by%20setting%20%60rateLimit%3A%20%7B%20enabled%3A%20false%20%7D%60%20in%20your%20config%2C%20or%20use%20a%20more%20robust%20custom%20storage%20backend%20for%20rate%20limiting%20if%20you%20expect%20high%20concurrency%20%5B%5Bsource%5D%5D%28https%3A//github.com/better-auth/better-auth/issues/5070%29.%20If%20you%20use%20API%20keys%2C%20make%20sure%20you%20aren%27t%20double-counting%20requests%20%28see%20%60enableSessionForAPIKeys%3A%20true%60%29.%20For%20production%2C%20consider%20limiting%20parallelism%20on%20the%20client%2C%20adding%20retry%20logic%2C%20or%20using%20a%20queue/throttle%20to%20avoid%20flooding%20the%20server%20%5B%5Bsource%5D%5D%28https%3A//github.com/better-auth/better-auth/issues/4609%29.%0A%0AIf%20you%20need%20to%20debug%20further%2C%20check%20your%20logs%20for%20the%20actual%20stack%20trace%20of%20the%20500%20error%E2%80%94this%20will%20help%20pinpoint%20if%20it%27s%20a%20storage%20backend%20failure%2C%20a%20race%20condition%2C%20or%20something%20else.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6335)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2402