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
Delete user state when they close the tab. Add an unload event for the demo and a delete_callback on gr.State to let developers control how resources are cleaned up #7829
Conversation
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-builds.s3.amazonaws.com/fb4ff449b2cad1ba38e5117261cb6a76ac43e2cc/gradio-4.24.0-py3-none-any.whl Install Gradio Python Client from this PR pip install "gradio-client @ git+https://github.com/gradio-app/gradio@fb4ff449b2cad1ba38e5117261cb6a76ac43e2cc#subdirectory=client/python" |
🦄 change detectedThis Pull Request includes changes to the following packages.
With the following changelog entry.
|
068afe2
to
d1fc1b4
Compare
6aec8fe
to
9ac8fb0
Compare
""" | ||
|
||
async def iterator(): | ||
while True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! How's that possible though? An asyncio.CancelledError should be raised when the network disconnects right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe chrome keeps the connections alive but doesn't allow new requests to go through?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's what I was thinking as well
cc @pseudotensor - I think you had asked for automatic state deletion. |
Nice, I can remove my work-around involving a web socket if this works. I'm still waiting for the k8 nginx proxy stuff to be fixed before moving forward with gradio 4 in general. Also the audio streaming regression isn't fixed yet. |
@@ -159,6 +159,7 @@ def __init__( | |||
self.sse_url = urllib.parse.urljoin( | |||
self.src, utils.SSE_URL_V0 if self.protocol == "sse" else utils.SSE_URL | |||
) | |||
self.heartbeat_url = urllib.parse.urljoin(self.src, utils.HEARTBEAT_URL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to include this in the clients as well!
It looks like the python unit tests were timing out? Rerunning.. |
611df7c
to
ae7bcb8
Compare
81af45b
to
a2eac46
Compare
@abidlabs I just pushed up your suggestion (state deleted 60 minutes after client disconnects) and made other minor tweaks (like highlight changelog)! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super cool PR! LGTM @freddyaboulton
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Thanks for the review @abidlabs !!! |
Description
This PR adds some features to address the problem of state being accumulated in the gradio server indefinitely.
Changes:
time_to_live
parameter togr.State
. This is how long the state will be held onto in the server after its most recent creation/update. This is for cases where users minimize the tab and forget about it.delete_callback
parameter togr.State
to let developers do any additional clean up actions when state is deleted, e.g. delete tensors from GPU memory. This parameter accepts a function that takes one parameter, the state value.Blocks.unload
method to set an "event" that is triggered when a user closes the page. The goal is to perform any additional clean up not pertaining to gr.State when a user leaves the page. The state is automatically deleted even if no unload event is attached to the demo. This event only accepts one parameter, the function to run. No inputs and outputs are accepted because there is no longer a client connection to receive or send data to.To test, try the
demo/state_cleanup
ordemo/unload_event_test
demos.Closes: #7517
🎯 PRs Should Target Issues
Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.
Not adhering to this guideline will result in the PR being closed.
Tests
PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests:
bash scripts/run_all_tests.sh
You may need to run the linters:
bash scripts/format_backend.sh
andbash scripts/format_frontend.sh