-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Bugfix: Fix quart threadedrunner stop graceful shutdown #3824
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
Open
joschrag
wants to merge
3
commits into
plotly:dev
Choose a base branch
from
joschrag:fix/quart-threadedrunner-stop-graceful-shutdown
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import threading | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from dash import Dash, Input, Output, dcc, html | ||
| from dash.testing.application_runners import ThreadedRunner | ||
|
|
||
|
|
||
| def test_quart_threaded_runner_stop_is_graceful_and_bounded(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think we could make this test generic and parametrize with the three backends? |
||
| """Regression test: ``ThreadedRunner.stop()`` must not hang for a Quart app. | ||
|
|
||
| ``stop()`` only had a graceful-shutdown branch for FastAPI (keyed on | ||
| ``_uvicorn_server``). A Quart app fell through to ``thread.kill()`` followed | ||
| by an unbounded ``thread.join()``. The server thread is parked in a blocking | ||
| syscall (IOCP on Windows, epoll on POSIX), so the injected ``SystemExit`` is | ||
| not delivered promptly and ``join()`` can block forever. | ||
|
|
||
| ``stop()`` now signals the Quart backend's cooperative shutdown event | ||
| (``backend._ws_shutdown_event``) on the server's own loop and joins bounded | ||
| by ``stop_timeout``. | ||
| """ | ||
| pytest.importorskip("quart", reason="Quart extra dependencies are not installed") | ||
| pytest.importorskip("hypercorn", reason="hypercorn is not installed") | ||
|
|
||
| app = Dash(__name__, backend="quart") | ||
| app.layout = html.Div( | ||
| [dcc.Input(id="input", value="initial value"), html.Div(id="output")] | ||
| ) | ||
|
|
||
| @app.callback(Output("output", "children"), Input("input", "value")) | ||
| def update_output(value): | ||
| return value | ||
|
|
||
| runner = ThreadedRunner(stop_timeout=3) | ||
| runner.host = "127.0.0.1" | ||
| runner.start(app, host="127.0.0.1") | ||
|
|
||
| try: | ||
| # Sanity: a Quart app does NOT take the FastAPI graceful branch ... | ||
| assert not hasattr(app, "_uvicorn_server") | ||
| # ... but its backend does expose the cooperative shutdown switch. | ||
| assert getattr(app.backend, "_ws_shutdown_event", None) is not None | ||
|
|
||
| # Run stop() under a watchdog so a regression fails fast instead of | ||
| # wedging the whole suite. The graceful path never calls thread.kill(), | ||
| # so this watchdog thread is safe; a regression to the kill path would | ||
| # inject SystemExit here and leave `done` unset -> the assertion below | ||
| # fails (bounded) rather than hanging forever. | ||
| done = threading.Event() | ||
|
|
||
| def _stop(): | ||
| runner.stop() | ||
| done.set() | ||
|
|
||
| start = time.monotonic() | ||
| threading.Thread(target=_stop, daemon=True).start() | ||
| returned = done.wait(timeout=runner.stop_timeout + 5) | ||
| elapsed = time.monotonic() - start | ||
|
|
||
| assert returned, ( | ||
| "ThreadedRunner.stop() did not return for a Quart app within " | ||
| f"{runner.stop_timeout + 5}s -- regression of the teardown hang" | ||
| ) | ||
| assert elapsed < runner.stop_timeout + 2 | ||
| assert not runner.thread.is_alive() | ||
| assert runner.started is False | ||
| finally: | ||
| if runner.started: | ||
| try: | ||
| runner.stop() | ||
| except Exception: # pylint: disable=broad-except | ||
| pass | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could change these checks to be more clear and not dependent on private variables checks, we have the server_type attribute