gradio-app/gradio

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

Closed this issue · 2 comments

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? 🔎

  • I have searched and found no 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

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.

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