[GH-ISSUE #3150] better-auth is contributing to memory leak with Sentry and Sveltekit #18124

Closed
opened 2026-04-15 16:30:31 -05:00 by GiteaMirror · 11 comments
Owner

Originally created by @tehnrd on GitHub (Jun 23, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3150

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

This repo has full steps and code to reproduce: https://github.com/tehnrd/sentry-mem-leak

Current vs. Expected behavior

Memory continues to grow and grow. Memory should not continue to grow.

What version of Better Auth are you using?

1.2.10

Provide environment information

Mac OSX
Node 22 LTS
"@sentry/sveltekit": "9.30.0",
"better-auth": "1.2.10"

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({});

Additional context

The problem appears to be this line: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/integrations/svelte-kit.ts#L23

After upgrading Sentry to 9.28.1 this starts to cause memory leaks. I do not entirely understand why.

I think the fix is simply moving the following code outside of the svelteKitHandler function as there is no reason this import needs to run on every single request:

//@ts-expect-error
	const { building } = await import("$app/environment")
		.catch((e) => {})
		.then((m) => m || {});

I will try to open a PR soon, but wanted to log the issue if someone else can jump on this.

Related sentry issue: https://github.com/getsentry/sentry-javascript/issues/16674#issuecomment-2996188619

Originally created by @tehnrd on GitHub (Jun 23, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3150 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce This repo has full steps and code to reproduce: https://github.com/tehnrd/sentry-mem-leak ### Current vs. Expected behavior Memory continues to grow and grow. Memory should not continue to grow. ### What version of Better Auth are you using? 1.2.10 ### Provide environment information ```bash Mac OSX Node 22 LTS "@sentry/sveltekit": "9.30.0", "better-auth": "1.2.10" ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({}); ``` ### Additional context The problem appears to be this line: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/integrations/svelte-kit.ts#L23 After upgrading Sentry to 9.28.1 this starts to cause memory leaks. I do not entirely understand why. I think the fix is simply moving the following code outside of the svelteKitHandler function as there is no reason this import needs to run on every single request: ```javascript //@ts-expect-error const { building } = await import("$app/environment") .catch((e) => {}) .then((m) => m || {}); ``` I will try to open a PR soon, but wanted to log the issue if someone else can jump on this. Related sentry issue: https://github.com/getsentry/sentry-javascript/issues/16674#issuecomment-2996188619
GiteaMirror added the lockedbug labels 2026-04-15 16:30:31 -05:00
Author
Owner

@tehnrd commented on GitHub (Jun 23, 2025):

It looks like import { building } from "$app/environment"; used to be implemented as a top-level import, but then it was moved here.

<!-- gh-comment-id:2997108123 --> @tehnrd commented on GitHub (Jun 23, 2025): It looks like `import { building } from "$app/environment";` used to be [implemented as a top-level import](https://github.com/better-auth/better-auth/blame/c9644cc9861e4ac09d06029ba8d963fa8082c690/packages/better-auth/src/integrations/svelte-kit.ts), but then it was moved [here](https://github.com/better-auth/better-auth/commit/78978d4a32978c5c952bec924dce9f5a91bb54ee).
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

@ping-maxwell @Bekacru Sorry to @ mention, I know how hectic startup life is, but can you take a look at this one? PR is ready to go. There is a nasty memory leak with better-auth with Sentry and the way better-auth is doing dynamic imports in ESM builds for every single http request with SvelteKit.

<!-- gh-comment-id:3000449837 --> @tehnrd commented on GitHub (Jun 24, 2025): @ping-maxwell @Bekacru Sorry to @ mention, I know how hectic startup life is, but can you take a look at this one? PR is ready to go. There is a nasty memory leak with better-auth with [Sentry](https://github.com/getsentry/sentry-javascript) and the way better-auth is doing dynamic imports in ESM builds for every single http request with SvelteKit.
Author
Owner

@ping-maxwell commented on GitHub (Jun 24, 2025):

Hello, can you try installing this and LMK if it solves your issue?
npm i https://pkg.pr.new/better-auth/better-auth@3161

<!-- gh-comment-id:3001232942 --> @ping-maxwell commented on GitHub (Jun 24, 2025): Hello, can you try installing this and LMK if it solves your issue? npm i https://pkg.pr.new/better-auth/better-auth@3161
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

@ping-maxwell that did not solve the memory leak....which is odd( nevermind, I see the PR is different, I see issue and left comments)

Image
<!-- gh-comment-id:3001892319 --> @tehnrd commented on GitHub (Jun 24, 2025): @ping-maxwell that did not solve the memory leak....~~which is odd~~( nevermind, I see the PR is different, I see issue and left comments) <img width="1186" alt="Image" src="https://github.com/user-attachments/assets/3ee8e907-ae55-42a1-a327-fc46c824c00f" />
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

building = resolve.building; in your PR is not working as expected. What was the issue with my original approach?

Resolve is a promise that needs to be called, not set with a value. Can move comment to your PR.

<!-- gh-comment-id:3001953338 --> @tehnrd commented on GitHub (Jun 24, 2025): `building = resolve.building;` in your PR is not working as expected. What was the issue with my original approach? Resolve is a promise that needs to be called, not set with a value. Can move [comment to your PR.](https://github.com/better-auth/better-auth/pull/3161)
Author
Owner

@ping-maxwell commented on GitHub (Jun 24, 2025):

Oh the cubic dev bot changed the import variable name but didn't change the building = resolve.building part..
I'll fix that up, one moment

<!-- gh-comment-id:3001965809 --> @ping-maxwell commented on GitHub (Jun 24, 2025): Oh the cubic dev bot changed the import variable name but didn't change the `building = resolve.building` part.. I'll fix that up, one moment
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

#shakesfist at AI!

<!-- gh-comment-id:3001966457 --> @tehnrd commented on GitHub (Jun 24, 2025): #shakesfist at AI!
Author
Owner

@ping-maxwell commented on GitHub (Jun 24, 2025):

Oh you already had a PR, awesome!
And your implementation works right?

<!-- gh-comment-id:3001970788 --> @ping-maxwell commented on GitHub (Jun 24, 2025): Oh you already had a PR, awesome! And your implementation works right?
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

Yup, my PR works! Your latest change also didn't work :-/ , as it seems like it comes back as empty object for non-truthy instead of false, which just causes the if check to fail keep being true

Image
<!-- gh-comment-id:3001984212 --> @tehnrd commented on GitHub (Jun 24, 2025): Yup, my PR works! Your latest change also didn't work :-/ , as it seems like it comes back as empty object for non-truthy instead of false, which just causes the `if` check to fail keep being true <img width="671" alt="Image" src="https://github.com/user-attachments/assets/ea5184e6-3c77-4529-b3bc-6c64349b485a" />
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

Oh you already had a PR, awesome! And your implementation works right?

I tested with npm i https://pkg.pr.new/better-auth/better-auth@3152 from the my PR and the memory leak is gone.

<!-- gh-comment-id:3001995906 --> @tehnrd commented on GitHub (Jun 24, 2025): > Oh you already had a PR, awesome! And your implementation works right? I tested with npm i https://pkg.pr.new/better-auth/better-auth@3152 from the my PR and the memory leak is gone.
Author
Owner

@tehnrd commented on GitHub (Jun 24, 2025):

Thank you both for jumping on this one, my servers are going to be very grateful once upgraded.

Image
<!-- gh-comment-id:3002106020 --> @tehnrd commented on GitHub (Jun 24, 2025): Thank you both for jumping on this one, my servers are going to be very grateful once upgraded. <img width="787" alt="Image" src="https://github.com/user-attachments/assets/01cd2b66-1d95-4df6-b23f-77f2ef36ef3b" />
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18124