From d7ab2760bee38cd0151eb7f62505d69a4be264b9 Mon Sep 17 00:00:00 2001 From: kaiLake <19817578079@163.com> Date: Tue, 17 Nov 2026 18:47:21 +0800 Subject: [PATCH] fix(runtime): disable debug mode in the Flask service Keep the directly launched service's debugger and reloader disabled, with regression coverage for startup settings and normal HTTP behavior. --- runtime/python/websocket/funASR-Service.py | 2 +- tests/test_flask_service_debug.py | 95 ++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/test_flask_service_debug.py diff --git a/runtime/python/websocket/funASR-Service.py b/runtime/python/websocket/funASR-Service.py index a8d8803b80..39ec07585f 100644 --- a/runtime/python/websocket/funASR-Service.py +++ b/runtime/python/websocket/funASR-Service.py @@ -275,4 +275,4 @@ def calculate_similarity(): }) if __name__ == '__main__': - app.run(host='0.0.0.0', port=10099, debug=True) + app.run(host='0.0.0.0', port=10099, debug=False) diff --git a/tests/test_flask_service_debug.py b/tests/test_flask_service_debug.py new file mode 100644 index 0000000000..7981aec789 --- /dev/null +++ b/tests/test_flask_service_debug.py @@ -0,0 +1,95 @@ +from pathlib import Path +import runpy +import sys +import types +from unittest.mock import Mock + +import pytest + +pytest.importorskip("flask") +pytest.importorskip("numpy") +pytest.importorskip("scipy") + +SERVICE_PATH = ( + Path(__file__).resolve().parents[1] / "runtime/python/websocket/funASR-Service.py" +) + + +@pytest.fixture +def service(monkeypatch, tmp_path): + # Stub model dependencies, but execute the real entrypoint and Flask.run. + modules = { + name: types.ModuleType(name) + for name in ( + "funasr", + "modelscope", + "modelscope.pipelines", + "modelscope.utils", + "modelscope.utils.constant", + "openai", + "torch", + "torchaudio", + ) + } + modules["funasr"].AutoModel = Mock() + modules["modelscope.pipelines"].pipeline = Mock() + modules["modelscope.utils.constant"].Tasks = types.SimpleNamespace() + modules["openai"].OpenAI = Mock() + modules["torch"].cuda = types.SimpleNamespace(is_available=lambda: False) + for name, module in modules.items(): + monkeypatch.setitem(sys.modules, name, module) + monkeypatch.chdir(tmp_path) + monkeypatch.delenv("FLASK_RUN_FROM_CLI", raising=False) + monkeypatch.setenv("FLASK_SKIP_DOTENV", "1") + run_server = Mock() + monkeypatch.setattr("werkzeug.serving.run_simple", run_server) + + def load(): + namespace = runpy.run_path(str(SERVICE_PATH), run_name="__main__") + return namespace["app"], run_server + + return load + + +@pytest.mark.parametrize("debug_env", [None, "0", "1"]) +def test_direct_start_disables_debugger_and_reloader(service, monkeypatch, debug_env): + if debug_env is None: + monkeypatch.delenv("FLASK_DEBUG", raising=False) + else: + monkeypatch.setenv("FLASK_DEBUG", debug_env) + + app, run_server = service() + + run_server.assert_called_once() + args, options = run_server.call_args + assert args == ("0.0.0.0", 10099, app) + assert app.debug is False + assert options["use_debugger"] is False + assert options["use_reloader"] is False + + +def test_routes_still_work_with_debug_disabled(service): + app, _ = service() + client = app.test_client() + + for endpoint in ("/Register_Speaker", "/AsrCamWithIdentify"): + response = client.post(endpoint) + assert response.status_code == 400 + assert response.get_json() == {"error": "No audio file provided"} + + response = client.post( + "/calculate_similarity", json={"emb1": [1.0, 0.0], "emb2": [1.0, 0.0]} + ) + assert response.status_code == 200 + assert response.get_json() == {"status": "success", "result": pytest.approx(1.0)} + + +def test_unhandled_route_error_returns_generic_500(service): + app, _ = service() + + response = app.test_client().post("/calculate_similarity", json={}) + + assert response.status_code == 500 + assert b"Internal Server Error" in response.data + assert b"Traceback" not in response.data + assert b"Werkzeug Debugger" not in response.data