|
| 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 | +@pytest.mark.parametrize("backend", ["flask", "quart", "fastapi"]) |
| 11 | +def test_threaded_runner_stop_is_bounded(backend): |
| 12 | + """Regression test: ``ThreadedRunner.stop()`` must return within the timeout |
| 13 | + for every backend instead of wedging the suite -- both the graceful ASGI |
| 14 | + path (quart/fastapi) and Flask's ``thread.kill()`` path. |
| 15 | + """ |
| 16 | + |
| 17 | + if backend == "quart": |
| 18 | + pytest.importorskip( |
| 19 | + "quart", reason="Quart extra dependencies are not installed" |
| 20 | + ) |
| 21 | + pytest.importorskip("hypercorn", reason="hypercorn is not installed") |
| 22 | + elif backend == "fastapi": |
| 23 | + pytest.importorskip( |
| 24 | + "fastapi", reason="fastapi extra dependencies are not installed" |
| 25 | + ) |
| 26 | + |
| 27 | + app = Dash(__name__, backend=backend) |
| 28 | + app.layout = html.Div( |
| 29 | + [dcc.Input(id="input", value="initial value"), html.Div(id="output")] |
| 30 | + ) |
| 31 | + |
| 32 | + @app.callback(Output("output", "children"), Input("input", "value")) |
| 33 | + def update_output(value): |
| 34 | + return value |
| 35 | + |
| 36 | + runner = ThreadedRunner(stop_timeout=3) |
| 37 | + runner.host = "127.0.0.1" |
| 38 | + |
| 39 | + # Run stop() in a watchdog so a regression fails the assertion instead of |
| 40 | + # hanging the suite. The watchdog is started BEFORE runner.start(): Flask's |
| 41 | + # stop() -> thread.kill() injects SystemExit into every thread created after |
| 42 | + # the KillerThread, so a watchdog spawned afterwards would kill itself. |
| 43 | + # Starting it first lands it in KillerThread._old_threads, which kill() skips |
| 44 | + # -- harmless for the graceful backends, which never call kill(). |
| 45 | + done = threading.Event() |
| 46 | + go = threading.Event() |
| 47 | + |
| 48 | + def _stop(): |
| 49 | + go.wait() |
| 50 | + runner.stop() |
| 51 | + done.set() |
| 52 | + |
| 53 | + threading.Thread(target=_stop, daemon=True).start() |
| 54 | + runner.start(app, host="127.0.0.1") |
| 55 | + |
| 56 | + try: |
| 57 | + start = time.monotonic() |
| 58 | + go.set() |
| 59 | + returned = done.wait(timeout=runner.stop_timeout + 5) |
| 60 | + elapsed = time.monotonic() - start |
| 61 | + |
| 62 | + assert returned, ( |
| 63 | + f"ThreadedRunner.stop() did not return for a {backend} app within " |
| 64 | + f"{runner.stop_timeout + 5}s -- regression of the teardown hang" |
| 65 | + ) |
| 66 | + assert elapsed < runner.stop_timeout + 2 |
| 67 | + assert not runner.thread.is_alive() |
| 68 | + assert runner.started is False |
| 69 | + finally: |
| 70 | + if runner.started: |
| 71 | + try: |
| 72 | + runner.stop() |
| 73 | + except Exception: # pylint: disable=broad-except |
| 74 | + pass |
0 commit comments