Skip to content
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

How does State clean up and do state objects persist in memory after a user closes their window? #7227

Closed
1 task done
NickGeneva opened this issue Jan 30, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@NickGeneva
Copy link

NickGeneva commented Jan 30, 2024

Describe the bug

This is a general question / bug I'm wondering about session States in gradio.

I'm trying to detect when a user's session exists (related to clean up tasks: e.g. #3950 and #4016).

This led me to trying to use the State of the session to store data but when testing I noticed that seems garbage collection of session states do no occur until the server is closed. Namely if I give a gr.State a class with a __del__ function, this will not get triggered until the server is closes for all sessions.

How is gradio handling the clean up of session states and are they cleared from memory when users close the window?

Thank you for the support.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr


class StateClass:
    def __init__(self):
        print("Init called")
    
    def __del__(self):
        print('Destructor called')

def run_app():
    gr.close_all()
    with gr.Blocks() as demo:
        my_state = gr.State(StateClass)
        gr.Button("Hello world")

    demo.launch(server_name="0.0.0.0", debug=True)

if __name__ == "__main__":
    run_app()

Screenshot

No response

Logs

Running the sample script, opening a browser window and refreshing twice gives:

Init called
Running on local URL:  http://0.0.0.0:7860

To create a public link, set `share=True` in `launch()`.
Init called
Init called
Init called

Closing the server with ctrl+c then produces (hangs after first ctrl+c):

^CKeyboard interruption in main thread... closing server.
^CKeyboard interruption in main thread... closing server.
Destructor called
Destructor called
Destructor called

Note: I would typically expect the Destructor to get called every refresh, but this is not the case?

System Info

Python 3.10.13
>>> import gradio
>>> gradio.__version__
'4.16.0'

Severity

I can work around it

@NickGeneva NickGeneva added the bug Something isn't working label Jan 30, 2024
@abidlabs
Copy link
Member

Hi @NickGeneva that's correct, the State objects are not cleared from memory when a user leaves the browser session as we currently have no way of detecting that. They persist in memory until the Gradio app server is shut down.

@NickGeneva
Copy link
Author

Thanks for the info @abidlabs

So from a deployment standpoint, large data should not be stored in session states given that this will basically memory leak until the server gets restarted.

@abidlabs
Copy link
Member

abidlabs commented Jan 31, 2024

That's correct, we don't currently have a mechanism to clear memory or files. You could save the data to a file instead and have a background job to periodically clean it. This is something we'll build into Gradio down the line, but right now it would have to be a manual process.

@NickGeneva
Copy link
Author

Got it, yeah right now I have a timed caching mechanism that checks for older files past a certain timedelta and removes this that is stored in the global state. I attach this check onto every event so essentially users drive the amount the cache is checked.

Not the most efficient but I'm dealing with larger data movement on the backend so keeping things clean is important for my use case.

Look forward to better solutions in the future, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants