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

Stop running iterators when js client disconnects #7835

Merged
merged 7 commits into from Mar 27, 2024

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Mar 26, 2024

Description

If you run an iterator event and close the page the python function will keep running in the backend.

To test, run the following demo, click the button and close the page. Things will still be being printed to the console in main. On this branch, they do not. Also added a playwright test for this.

    def iterate():
        for i in range(8):
            print(f"UPDATE: {i}")
            yield i
            time.sleep(0.5)

    with gr.Blocks() as demo:
        with gr.Row():
            c_time2 = gr.Textbox(label="Iteration Step")
            btn = gr.Button("Start")
            btn.click(iterate, None, c_time2, api_name="start")


   demo.launch()

🎯 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

  1. 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

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Mar 26, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/68608040ca8d10d38bb626ac969134afe4e50c50/gradio-4.23.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@68608040ca8d10d38bb626ac969134afe4e50c50#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Mar 26, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Stop running iterators when js client disconnects

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@freddyaboulton freddyaboulton added the v: patch A change that requires a patch release label Mar 26, 2024
@freddyaboulton freddyaboulton marked this pull request as ready for review March 26, 2024 15:44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert these changes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need this for the 2e2 test actually! I don't know how else to verify the server process stopped running

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it! We don't use this demo in any of our docs so it should be fine

for i in range(steps):
print(f"Current step: {i}")
log_file.write_text(f"Current step: {i}\n")
time.sleep(0.5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this faster? So that we could reduce the time that we have to wait in the test

Suggested change
time.sleep(0.5)
time.sleep(0.2)

@@ -803,11 +803,11 @@ async def sse_stream(request: fastapi.Request):
message=str(e),
)
response = process_msg(message)
if response is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so we already had the deletion logic in place, but it was never called?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we yielded the response before the clean up logic happened so we never reached that point in the code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

"../../demo/cancel_events/cancel_events_output_log.txt",
"utf-8"
);
expect(data).not.toContain("Current step: 8");
Copy link
Member

@abidlabs abidlabs Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also ensure that at least one of the steps that should be logged are logged

Suggested change
expect(data).not.toContain("Current step: 8");
expect(data).toContain("Current step: 1");
expect(data).not.toContain("Current step: 8");

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable!

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few suggestions. Cool test!

@freddyaboulton
Copy link
Collaborator Author

Thanks for the review @abidlabs ! I addressed your comments so will merge once ci passes

@freddyaboulton freddyaboulton merged commit ee804b2 into main Mar 27, 2024
8 checks passed
@freddyaboulton freddyaboulton deleted the close-running-iterators branch March 27, 2024 17:41
@pngwn pngwn mentioned this pull request Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants