supabase-py: Supabase Python Client does not close connection & hangs

Describe the bug The Supabase Python client does not close its connection when it goes out of scope or is deleted, leading to connections persisting after tests are completed in a Django test suite.

To Reproduce Here’s an example of code that reproduces the behavior:

class JWTTest(TestCase):
    def setUp(self):
        self.supabase: SupabaseClient = create_client(os.environ['SUPABASE_URL'], os.environ['SUPABASE_KEY'])

    def tearDown(self):
        pass

    def test_jwt(self):
        # test code...

Steps to reproduce the behavior:

  1. Set up a Django test case that uses the Supabase Python client.
  2. Run a test method that uses the Supabase client.
  3. After the test is completed, the connection to Supabase does not close, preventing Django from cleaning up the test database.

Expected behavior The Supabase Python client should automatically close its connection when it goes out of scope or is deleted, allowing Django to clean up its test database after tests are run.

Desktop (please complete the following information):

  • OS: Mac OS 13.5
  • Python version: 3.11
  • Supabase Python client version: 1.0.3

Additional context This issue has caused problems in running Django test suites, as it prevents Django from deleting its test database due to the persistent connection. I haven’t found any valid workarounds besides keyboard interrupt (e.g., forcing garbage collection), it would be better if the Supabase client could handle this automatically. Additionally, I tested this in a minimal script that just initializes the Supabase client, and the behavior is the same.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 10
  • Comments: 15 (3 by maintainers)

Most upvoted comments

FWIW’s in my case if I do end my code with client.auth.sign_out() it doesn’t hang any more, but I am not sure that’s the behaviour we want? 🤔

can concur this happened to me to when using it with pytest, and I also use signInWithPassword

I have tested this and calling sign_out() fixes the issue for me. The reason it hangs when you don’t call sign_out() is because there is a timer thread which is used to refresh the auth token which is only closed when sign_out() is called. You can see a related issue here which was closed https://github.com/supabase-community/supabase-py/issues/451. I have also included a screen recording of the hanging when sign_out() is not called and no hanging when sign_out() is called.

https://github.com/supabase-community/supabase-py/assets/79497/7aa0d9e5-0d33-4edf-a25f-08737c568108

I am experiencing this same issue

@silentworks I don’t have an example repo, but I found this ticket after a search as I encountered this problem in a very simple standalone script (also mentioned above by @djmango in the issue description). The user email@example.com exists in the authentication table and after printing the user object the script hangs.

import os
from supabase import create_client

# create client
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)

# sign in
random_email: str = "email@example.com"
random_password: str = "big-secret"
user = client.auth.sign_in_with_password(
    {"email": random_email, "password": random_password}
)
print(user)

After Ctrl-c the following trace is displayed:

Exception ignored in: <module 'threading' from '.../3.9.13/lib/python3.9/threading.py'>
Traceback (most recent call last):
  File ".../3.9.13/lib/python3.9/threading.py", line 1477, in _shutdown
    lock.acquire()
KeyboardInterrupt: 

Hope that helps.

@silentworks I don’t have an example repo, but I found this ticket after a search as I encountered this problem in a very simple standalone script (also mentioned above by @djmango in the issue description). The user email@example.com exists in the authentication table and after printing the user object the script hangs.

import os
from supabase import create_client

# create client
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)

# sign in
random_email: str = "email@example.com"
random_password: str = "big-secret"
user = client.auth.sign_in_with_password(
    {"email": random_email, "password": random_password}
)
print(user)

After Ctrl-c the following trace is displayed:

Exception ignored in: <module 'threading' from '.../3.9.13/lib/python3.9/threading.py'>
Traceback (most recent call last):
  File ".../3.9.13/lib/python3.9/threading.py", line 1477, in _shutdown
    lock.acquire()
KeyboardInterrupt: 

Hope that helps.

Sorry @silentworks i missed the reply but I would say the client needs to use a daemon thread to make sure it does not hang the whole application.

https://docs.python.org/3/library/threading.html#threading.Thread.daemon

Also stumbled into this issue whenever accessing anything supabase.auth-related (like supabase.auth.sign_up to create a test user).

@vidbina Same exact issue for me, supabase.auth was the core problem. I tried that in the seed but it didn’t seem to work for me, the user wasn’t “confirmed” so I couldn’t access them. I ended up doing this, which did work for me, and allowed for the teardown you were looking for

        res = requests.post(signup_url, json={"email": user["email"], "password": user["password"]}, headers=headers)
        # User may already exist. That's OK
        if res.status_code != 200:
            return
        user_id = res.json()["user"]["id"]
        client_ = create_client(SUPABASE_URL, SUPABASE_SECRET)
        client_.table("api_tokens").insert(
            {"user_id": user_id, "token": user["token"], "expiry": "2099-01-01"}
        ).execute()

and then I tear them down with another post request at the end.

Also stumbled into this issue whenever accessing anything supabase.auth-related (like supabase.auth.sign_up to create a test user).

The workaround I settled for was seeding a test user such that I don’t have to call any of the supabase.auth functions for now.

INSERT INTO auth.users
(id, instance_id, aud, role, email, encrypted_password, created_at, updated_at, email_confirmed_at, confirmation_sent_at, raw_app_meta_data)
VALUES
('d2ab3142-6538-4e6e-a43b-52a0ffb3ea93', '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'test@example.org', 'ENCRYPTED', now(), now(), now(), now(), '{"provider":"email","providers":["email"]}')
RETURNING *;

My sloppy workaround was adding this to seed for now, but a cleaner setup would be to call this during setup and then removing it during teardown instead. 🙈