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

fix: handling SIGINT correctly in reload.py, single entrance of block_thread in blocks.py #8158

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions gradio/blocks.py
Expand Up @@ -2427,18 +2427,21 @@ def reverse(text):
}
analytics.launched_analytics(self, data)

# Block main thread if debug==True
if debug or int(os.getenv("GRADIO_DEBUG", "0")) == 1 and not wasm_utils.IS_WASM:
self.block_thread()
# Block main thread if running in a script to stop script from exiting
is_in_interactive_mode = bool(getattr(sys, "ps1", sys.flags.interactive))

# Block main thread if debug==True
if (
not prevent_thread_lock
and not is_in_interactive_mode
# In the Wasm env, we don't have to block the main thread because the server won't be shut down after the execution finishes.
# Moreover, we MUST NOT do it because there is only one thread in the Wasm env and blocking it will stop the subsequent code from running.
debug
or int(os.getenv("GRADIO_DEBUG", "0")) == 1
and not wasm_utils.IS_WASM
or (
# Block main thread if running in a script to stop script from exiting
not prevent_thread_lock
and not is_in_interactive_mode
# In the Wasm env, we don't have to block the main thread because the server won't be shut down after the execution finishes.
# Moreover, we MUST NOT do it because there is only one thread in the Wasm env and blocking it will stop the subsequent code from running.
and not wasm_utils.IS_WASM
)
):
self.block_thread()

Expand Down
6 changes: 5 additions & 1 deletion gradio/cli/commands/reload.py
Expand Up @@ -126,7 +126,11 @@ def main(
GRADIO_WATCH_DEMO_PATH=str(path),
),
)
popen.wait()
if popen.poll() is None:
try:
popen.wait()
except (KeyboardInterrupt, OSError):
print("gradio-cli: Waiting gradio main thread cleaning.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @Tiger3018 ! This contribution looks good to me but I don't think we need print statement here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the original implementation, the typer module will print Aborted! (See this line in typer module) and silently exit. This PR will remove the Aborted! echo and I don't know if an alternative text should be there.

Maybe we should just follow the implementation of gradio module, and use pass in this line instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, pass is what I had in mind

Copy link
Collaborator

Choose a reason for hiding this comment

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

Pushed that up so we can merge this in. Thanks!



if __name__ == "__main__":
Expand Down