fix: serialize TTSBridge IO and TTSService lifecycle to prevent concurrency crashes

- TTSBridge.synthesize wraps stdin/stdout request-response in asyncio.Lock;
  concurrent calls previously caused 'readuntil() called while another
  coroutine is already waiting' and ENOENT (temp dir deleted while bridge
  still reading input.txt)
- TTSService.start/stop guarded by lifecycle lock to prevent race from
  rapid Start clicks creating duplicate bridge/worker/danmaku connections
- Verified: 20 concurrent synthesize calls all succeed with correct WAV
- Rebuild portable zip (83.8 MB)
This commit is contained in:
2026-08-08 15:38:08 +08:00
parent f27bcfb055
commit f4c99b1692
2 changed files with 69 additions and 64 deletions
+3
View File
@@ -37,6 +37,7 @@ class TTSService:
self._shutdown_event: asyncio.Event | None = None
self._worker_task: asyncio.Task | None = None
self._cookies: dict[str, str] | None = None
self._lifecycle_lock = asyncio.Lock()
self.running = False
self.start_time: float = 0
self.messages_processed = 0
@@ -48,6 +49,7 @@ class TTSService:
self._cookies = cookies
async def start(self, config: dict) -> None:
async with self._lifecycle_lock:
if self.running:
raise RuntimeError("Already running")
room_id = config["room_id"]
@@ -73,6 +75,7 @@ class TTSService:
self._worker_task = asyncio.create_task(self._tts_worker(convert_numbers))
async def stop(self) -> None:
async with self._lifecycle_lock:
if not self.running:
return
self.running = False
+2
View File
@@ -46,6 +46,7 @@ class TTSBridge:
self._bridge_script = bridge_script or _resolve_path("tts_bridge.js")
self._node_exe = _get_node_exe()
self._stderr_task: asyncio.Task | None = None
self._io_lock = asyncio.Lock()
async def start(self) -> None:
if not pathlib.Path(self._bridge_script).exists():
@@ -96,6 +97,7 @@ class TTSBridge:
if self._process is None or self._process.stdin is None:
raise RuntimeError("Bridge not started. Call start() first.")
async with self._io_lock:
tmpdir = pathlib.Path(tempfile.mkdtemp(prefix="tts_"))
try:
input_path = tmpdir / "input.txt"