gradio-app / gradio

Build and share delightful machine learning apps, all in Python. ๐ŸŒŸ Star to support our work!
http://www.gradio.app
Apache License 2.0
29.84k stars 2.22k forks source link

Delete callback in gr.State is not called on reload or tab close #8241

Closed bergerchris closed 2 weeks ago

bergerchris commented 3 weeks ago

Describe the bug

The docs say

Gradio automatically deletes state variables after a user closes the browser tab or refreshes the page, so this is useful for clearing state for potentially long running sessions.

and the delete callback should be called, when the state is deleted. But this never happens. Not with time_to_live nor with reload of the tab nor with closing the tab.

Have you searched existing issues? ๐Ÿ”Ž

Reproduction

import gradio as gr
import tempfile
import shutil
import os

def delete_tmp_dir(tmp_dir):
    if os.path.exists(tmp_dir):
        shutil.rmtree(tmp_dir)
        print(f"Deleted temporary directory: {tmp_dir}")
    else:
        print("Temporary directory already deleted")

def create_tmp_dir():
    tmp_dir = tempfile.mkdtemp()
    print(f"Created temporary directory: {tmp_dir}")
    return tmp_dir

def show_tmp_dir(tmp_dir):
    return f"Temporary Directory: {tmp_dir}"

with gr.Blocks() as demo:
    tmp_dir_display = gr.Markdown()
    tmp_dir_state = gr.State(create_tmp_dir, delete_callback=delete_tmp_dir, time_to_live=3600)
    check_btn = gr.Button("Check Temporary Directory")

    check_btn.click(show_tmp_dir, inputs=[tmp_dir_state], outputs=[tmp_dir_display])

demo.launch()

Screenshot

No response

Logs

No response

System Info

4.29.0

Severity

Blocking usage of gradio

freddyaboulton commented 3 weeks ago

Hi @bergerchris - the deletion will happen 60 minutes after the session is closed to allow users to reconnect in event of a temporary user outage.

abidlabs commented 2 weeks ago

Will close, let us know @bergerchris if there are any further issues