|
| 1 | +import threading |
| 2 | +import time |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from dash import Dash, Input, Output, dcc, html |
| 7 | +from dash.testing.application_runners import ThreadedRunner |
| 8 | + |
| 9 | + |
| 10 | +def test_quart_threaded_runner_stop_is_graceful_and_bounded(): |
| 11 | + """Regression test: ``ThreadedRunner.stop()`` must not hang for a Quart app. |
| 12 | +
|
| 13 | + ``stop()`` only had a graceful-shutdown branch for FastAPI (keyed on |
| 14 | + ``_uvicorn_server``). A Quart app fell through to ``thread.kill()`` followed |
| 15 | + by an unbounded ``thread.join()``. The server thread is parked in a blocking |
| 16 | + syscall (IOCP on Windows, epoll on POSIX), so the injected ``SystemExit`` is |
| 17 | + not delivered promptly and ``join()`` can block forever. |
| 18 | +
|
| 19 | + ``stop()`` now signals the Quart backend's cooperative shutdown event |
| 20 | + (``backend._ws_shutdown_event``) on the server's own loop and joins bounded |
| 21 | + by ``stop_timeout``. |
| 22 | + """ |
| 23 | + pytest.importorskip("quart", reason="Quart extra dependencies are not installed") |
| 24 | + pytest.importorskip("hypercorn", reason="hypercorn is not installed") |
| 25 | + |
| 26 | + app = Dash(__name__, backend="quart") |
| 27 | + app.layout = html.Div( |
| 28 | + [dcc.Input(id="input", value="initial value"), html.Div(id="output")] |
| 29 | + ) |
| 30 | + |
| 31 | + @app.callback(Output("output", "children"), Input("input", "value")) |
| 32 | + def update_output(value): |
| 33 | + return value |
| 34 | + |
| 35 | + runner = ThreadedRunner(stop_timeout=3) |
| 36 | + runner.host = "127.0.0.1" |
| 37 | + runner.start(app, host="127.0.0.1") |
| 38 | + |
| 39 | + try: |
| 40 | + # Sanity: a Quart app does NOT take the FastAPI graceful branch ... |
| 41 | + assert not hasattr(app, "_uvicorn_server") |
| 42 | + # ... but its backend does expose the cooperative shutdown switch. |
| 43 | + assert getattr(app.backend, "_ws_shutdown_event", None) is not None |
| 44 | + |
| 45 | + # Run stop() under a watchdog so a regression fails fast instead of |
| 46 | + # wedging the whole suite. The graceful path never calls thread.kill(), |
| 47 | + # so this watchdog thread is safe; a regression to the kill path would |
| 48 | + # inject SystemExit here and leave `done` unset -> the assertion below |
| 49 | + # fails (bounded) rather than hanging forever. |
| 50 | + done = threading.Event() |
| 51 | + |
| 52 | + def _stop(): |
| 53 | + runner.stop() |
| 54 | + done.set() |
| 55 | + |
| 56 | + start = time.monotonic() |
| 57 | + threading.Thread(target=_stop, daemon=True).start() |
| 58 | + returned = done.wait(timeout=runner.stop_timeout + 5) |
| 59 | + elapsed = time.monotonic() - start |
| 60 | + |
| 61 | + assert returned, ( |
| 62 | + "ThreadedRunner.stop() did not return for a Quart app within " |
| 63 | + f"{runner.stop_timeout + 5}s -- regression of the teardown hang" |
| 64 | + ) |
| 65 | + assert elapsed < runner.stop_timeout + 2 |
| 66 | + assert not runner.thread.is_alive() |
| 67 | + assert runner.started is False |
| 68 | + finally: |
| 69 | + if runner.started: |
| 70 | + try: |
| 71 | + runner.stop() |
| 72 | + except Exception: # pylint: disable=broad-except |
| 73 | + pass |
0 commit comments