supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
2.83k stars 219 forks source link

`GoTrueClient` Memory Leak #983

Closed ghost closed 2 months ago

ghost commented 2 months ago

Bug report

Describe the bug

Calling createClient results in a GoTrueClient memory leak on React Native. It may be the entire SupabaseClient, but I only tested with GoTrueClient after 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#L2040

It 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@0 and GoTrueClient@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) at 2024-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.txt

Expected behavior

Supabase clients should not be persisting when dereferenced; somehow garbage collection is being distorted.

Screenshots

https://github.com/supabase/supabase-js/assets/127700939/665eba59-b076-426a-801c-3407a2b01b03

System information

ghost commented 2 months ago

Moving to https://github.com/supabase/gotrue-js/issues/856 for visibility and better focus.