Skip to content

SSL socket created but not properly closed. #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alt-glitch opened this issue Nov 23, 2023 · 13 comments
Closed

SSL socket created but not properly closed. #621

alt-glitch opened this issue Nov 23, 2023 · 13 comments

Comments

@alt-glitch
Copy link

Describe the bug
On using supabase.table() in my FastAPI application, I think the library doesn't end up closing the SSL Socket leading to this error:

sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=13, family=2, type=1, proto=6, laddr=('10.191.23.204', 58278), raddr=('172.64.149.149', 443)>
ResourceWarning: Enable tracemalloc to get the object allocation traceback

To Reproduce
Steps to reproduce the behavior:

  1. Run the following FastAPI code
from fastapi import FastAPI
from supabase import Client, create_client

@app.get("/", status_code=200)
async def test():
    supabase = create_client(settings.SUPABASE_URL, settings.SUPABASE_SERVICE_KEY)
    response = supabase.from_("all_saved").select("*").execute()
    return response

run using uvicorn
2. Exit using Ctrl+C

❯ uvicorn main:app --port=8080 --app-dir src
INFO:     Started server process [107888]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
2023-11-23 15:50:25,477:INFO - HTTP Request: GET https://**.supabase.co/rest/v1/all_saved?select=%2A "HTTP/1.1 200 OK"
HTTP Request: GET https://**.supabase.co/rest/v1/all_saved?select=%2A "HTTP/1.1 200 OK"
INFO:     127.0.0.1:57198 - "GET / HTTP/1.1" 200 OK
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [107888]
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=8, family=2, type=1, proto=6, laddr=('10.191.23.204', 47824), raddr=('172.64.149.149', 443)>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
  1. You get the resourcewarning error

Expected behavior
Normal exit.

Desktop (please complete the following information):

  • OS: Linux (Ubuntu)
@silentworks
Copy link
Contributor

I've tested this out and can't replicate the issue you are getting. I'm using the latest v2.2.0 of the client library.

Kapture.2023-12-01.at.10.03.00.mp4

@alt-glitch
Copy link
Author

Hey, I still face this issue with the updated library in my fastapi application. I'll share a gist with you here.
Sorry for the delay!

@AdityaPunetha
Copy link

Same issue but with supabase.auth.set_session

@silentworks
Copy link
Contributor

@AdityaPunetha saying same issue doesn't really help. You need to provide a reproducible example of this. I have shown in my recording this is working without an issue so it's rather hard to fix a issue I nor anyone on the team have no way of reproducing.

@AdityaPunetha
Copy link

AdityaPunetha commented Jan 5, 2024

Hey I should have given more info. I'm working on an internal tool right now so i can't exactly share the complete code but below i have attached the relevant snippets. I will soon try to upload a codebase with reproducible example. Thank you for responding

Auth function

async def get_current_user(authorization: str = Security(token_key)):
    if not authorization:
        raise HTTPException(status_code=401, detail="Unauthorized")
    tokens = authorization.split(" ")

    # Check if there are two tokens (access token and refresh token)
    if len(tokens) != 2:
        raise HTTPException(
            status_code=401, detail="Invalid authorization header format"
        )

    # Extract the access token and refresh token
    access_token, refresh_token = tokens
    try:
        # supabase.auth.set_session()
        session = supabase.auth.set_session(
            access_token=access_token, refresh_token=refresh_token
        )
        return session
    except Exception as e:
        print(e)
        raise HTTPException(status_code=401, detail="Invalid token")

Route that I'm accessing

@router.get("/protected")
async def protected_route(session: dict = Depends(get_current_user)):
    return {"message": "You are authorized to access this route"}

Interrupting in bash doesn't stop it:
image (1)

Interrupting twice stops it in docker container:
image (2)

Libs:

fastapi==0.108.0
supabase==2.3.0

@AdityaPunetha
Copy link

Repo: https://github.com/AdityaPunetha/supabase-client-bug/tree/master
I have attached instructions in the readme

@ToniRV
Copy link

ToniRV commented Jan 5, 2024

+1!

@AdityaPunetha
Copy link

Were you able to reproduce it? @silentworks

@silentworks
Copy link
Contributor

silentworks commented Jan 11, 2024

@AdityaPunetha this is not the same as the original issue you are posting this on. You need to call signout somewhere in your code for the extra thread to stop there is another issue related to this on here #494 (comment).

@silentworks
Copy link
Contributor

@AdityaPunetha in your case you should be using the async client along with the Depends function. Use the latest 2.3.3 as this has a few other fixes in it. I've attached a video showing how this is done, I can also push your repo up as a fork if that works better for you.

Kapture.2024-01-11.at.22.15.34.mp4

@AdityaPunetha
Copy link

Thank you so much for dedicating your time to create this video. I've grasped the issue, and I believe it would be beneficial to others encountering the same problem if you could upload the new code. You can create the PR and i'll just merge it. I was having a hard time due to lack of documentation, your explanation proved incredibly helpful, Once again, Thank you

@silentworks
Copy link
Contributor

@AdityaPunetha thank you for the reproducible demo repo, it really helped in figuring out what the issue was that everyone was hitting. I've created a PR to your repo with the changes AdityaPunetha/supabase-client-bug#1

@silentworks
Copy link
Contributor

Closing this out as the original issue was resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants