From 24a39869c858e11bc6de40bd8175f9a1dd798fad Mon Sep 17 00:00:00 2001 From: locpham Date: Sun, 9 Aug 2026 11:23:36 +0000 Subject: [PATCH] fix: dau_engine de quy spawn vo han + load rat cham (ban 1.1.2) Trieu chung tu user: Task Manager day daw_engine process, port 8000->8005 leo thang, engine.log co engine moi moi ~2.5-3.3s trong nhieu phut. Goc re: app/core/vst_engine.py module-level: HAS_PEDALBOARD = check_pedalboard_safe() -> subprocess.run([sys.executable, '-c', 'import pedalboard'], timeout=2.0) Khi frozen, sys.executable = daw_engine.exe. PyInstaller bootloader bo qua '-c' va chay script nhung (desktop_engine.py = CA ENGINE) -> moi lan check thu vien lai khoi dong mot engine moi (timeout 2s moi check, block startup ~4s) -> engine con import app.main -> lai chay check -> spawn tiep => de quy vo han, hang chuc engine, may nghen, load 6-7.5s. Fix: - vst_engine: check_pedalboard_safe/check_pyfluidsynth_safe dung importlib.util.find_spec (micro-giay, khong spawn, hoat dong frozen). - desktop_engine.main(): guard phong thu - neu bi goi voi argv[1]=='-c' (dang bi dung nhu python interpreter) thi thoat ngay khong chay server. Verify tren Linux (engine frozen): - STARTUP: 6-7.5s -> 1.24-1.33s (3 lan lien tiep) - sau 15s: dung 1 engine, 0 process '-c import' (truoc: hang chuc) - 86 tests pass; test suite 34s -> 14s (may het bi spam). --- app/core/vst_engine.py | 36 +++++++++++++++++++----------------- desktop_engine.py | 7 +++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/app/core/vst_engine.py b/app/core/vst_engine.py index d8adb6b..2bb0420 100644 --- a/app/core/vst_engine.py +++ b/app/core/vst_engine.py @@ -49,27 +49,29 @@ def render_midi_events_to_audio(midi_events: list, sr: int = 44100, bpm: float = out_r /= max_peak return np.vstack([out_l, out_r]) -def check_pedalboard_safe(): - import subprocess, sys +def _module_available(name: str) -> bool: + """Kiem tra module co san khong — KHONG spawn subprocess. + + Truoc day dung subprocess.run([sys.executable, '-c', 'import X']) — + khi app dong goi (PyInstaller frozen), sys.executable = daw_engine.exe + -> subprocess chay CA ENGINE (bootloader bo qua '-c', chay desktop_engine) + -> moi lan check lai sinh ra engine moi -> de quy spawn vo han + (Task Manager day daw_engine, port 8000-8005 leo thang, load rat cham). + find_spec() nhanh (micro-giay) va hoat dong ca source lan frozen. + """ + import importlib.util try: - res = subprocess.run( - [sys.executable, "-c", "import pedalboard"], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2.0 - ) - return res.returncode == 0 - except Exception: + return importlib.util.find_spec(name) is not None + except (ImportError, AttributeError, ValueError): return False + +def check_pedalboard_safe(): + return _module_available("pedalboard") + + def check_pyfluidsynth_safe(): - import subprocess, sys - try: - res = subprocess.run( - [sys.executable, "-c", "import fluidsynth"], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2.0 - ) - return res.returncode == 0 - except Exception: - return False + return _module_available("fluidsynth") HAS_PEDALBOARD = check_pedalboard_safe() HAS_PYFLUIDSYNTH = check_pyfluidsynth_safe() diff --git a/desktop_engine.py b/desktop_engine.py index 5b010b3..67938c4 100644 --- a/desktop_engine.py +++ b/desktop_engine.py @@ -52,6 +52,13 @@ def _parent_alive(pid): def main(): + # PHONG THU (fix ban 1.1.2): neu exe bi goi nhu 'python -c ...' — vd code + # cu kiem tra thu vien bang subprocess.run([sys.executable, '-c', 'import X']) + # thi frozen sys.executable = daw_engine.exe -> se chay CA ENGINE o day. + # Thoat ngay, KHONG khoi dong server, tranh de quy spawn vo han. + if len(sys.argv) > 1 and sys.argv[1] == "-c": + return + os.environ.setdefault("SF_DESKTOP", "1") # Log dir tao SOM de stderr co the tro vao file (xem duoi).