-
Notifications
You must be signed in to change notification settings - Fork 538
Description
TLDR
This line is causing a memory leak:
https://github.com/supabase/gotrue-js/blob/15c7c8258b2d42d3378be4f7738c728a07523579/src/GoTrueClient.ts#L2040
setInterval will persist even if the object calling it (in this case, GoTrueClient) has been dereferenced. This prevents garbage collection (as setInterval is still holding a reference, even if the user has lost all references) and leads to the memory leak.
A solution seems to have been attempted, but it is not working in (at the very least) React Native, where I encountered this issue:
https://github.com/supabase/gotrue-js/blob/15c7c8258b2d42d3378be4f7738c728a07523579/src/GoTrueClient.ts#L2043
See Also
- the issue confirmed: https://stackoverflow.com/questions/41429030/does-setinterval-inside-an-object-prevent-garbage-collection-when-the-object-is
- potential solution: https://github.com/nbarrow-inspire-labs/bug-demo/blob/main/src/IntervalTimer.ts
- blocked in React Native: Hermes WeakRef Support in React Native facebook/react-native#42742
- docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef
History
I originally opened this here (#983) but moved once I discovered the setInterval issue.
Bug report
- I confirm this is a bug with Supabase, not with my own application.
- I confirm I have searched the Docs, GitHub Discussions, and Discord.
Describe the bug
Calling
createClientresults in aGoTrueClientmemory leak on React Native. It may be the entireSupabaseClient, but I only tested withGoTrueClientafter noticing massive memory issues. See demo repo and video below.EDIT!: I have tracked the issue down to this line of code:
https://github.com/supabase/gotrue-js/blob/15c7c8258b2d42d3378be4f7738c728a07523579/src/GoTrueClient.ts#L2040It looks like setInterval persists even after an object is dereferenced, avoiding garbage collection. Can we transfer this to @supabase/gotrue-js?
To Reproduce
I created a demo repo here. This is an extremely simplistic example; in our production app, we are using a router to navigate and noticed that (navigating between pages) we had memory leaks and supabase clients persisting, even though the page was no longer even being rendered and the supabase client should have been destroyed. I could not post our production app publicly, so I made this simple demo to show how even though I no longer have any reference to the client in my code (since it is created and destroyed after every button press), the auth client still exists and sends ticks in the background.
NOTE: as seen in the video below, after 2 button presses (and about 10 seconds) you can see log messages from two supabase clients (
GoTrueClient@0andGoTrueClient@1). These clients are not referenced anywhere; they were (a) created on a button press, then (b) all reference to them was lost. Garbage collection would usually take care of this (I have let it run for several minutes at a time and still had references; I've ever tried creating hundreds, losing the reference, and all hundreds of them persist for seemingly without end).Edit: I let the app run, without touching it at all, while writing this bug report (the same one shown in video, uninterrupted) and I was still seeing logs from
GoTrueClient@1(and@0) at2024-02-28T01:43:36.273Z, which is quite a while after in terms of waiting for some kind of garbage collection to have kicked in (assuming there was not a memory leak, but it clearly seems like there is). Here is a log dump showing the clients persisting for quite some time (untill the process was killed): logs.txtExpected behavior
Supabase clients should not be persisting when dereferenced; somehow garbage collection is being distorted.
Screenshots
1a6a7d82-4e3e-4979-9ae7-acf2326aba62.mp4
System information
- Version of supabase-js:
^2.39.7