Commit ef508645 authored by Aryan Noshirvani's avatar Aryan Noshirvani

Fix Demucs concurrency hang causing "Failed to fetch" mid-poll

Root cause of the reported polling failure: two remix jobs submitted
close together both landed in separate_stems() around the same time
and hung indefinitely - confirmed via direct DB inspection, both stuck
at "separating" with zero progress and low process CPU for 30+ seconds
straight, not just slow. Each call was also instantiating a brand new
demucs.api.Separator (reloading model weights from disk) every single
time, with no thread-safety guarantee from demucs/HF Hub's model
caching under concurrent access - the likely deadlock source.

Fixed by caching a single lazily-created Separator instance and
wrapping all separation calls in a threading.Lock, so only one
Demucs pass runs at a time process-wide; concurrent jobs now queue
instead of racing. This VPS's CPU has no SSE3/SSE4/AVX and would
thrash badly running two Demucs passes concurrently anyway, so
serializing is also just the right call for this hardware, independent
of the hang.

Verified with two remix jobs submitted ~simultaneously via Playwright
against separate browser contexts - both now complete cleanly instead
of hanging.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCxXmJVvDFJVHTUi4QgNzi
parent d8391794
......@@ -3,6 +3,7 @@ import hashlib
import json
import os
import shutil
import threading
import google.generativeai as genai
import librosa
......@@ -40,9 +41,29 @@ def _file_hash(file_path: str) -> str:
return h.hexdigest()[:24]
_separator = None
_separator_lock = threading.Lock()
def _get_separator():
global _separator
if _separator is None:
from demucs.api import Separator
_separator = Separator(model="htdemucs")
return _separator
def separate_stems(file_path: str) -> dict:
"""Runs Demucs source separation, caching results by file content hash so the
same song is never separated twice."""
same song is never separated twice.
Guarded by a process-wide lock: with two remix jobs submitted close together, both
landed in this function around the same time and hung indefinitely (no CPU activity,
no progress) instead of erroring - almost certainly concurrent access to demucs/the
HF Hub model cache, which isn't documented as thread-safe. On this VPS's weak,
no-AVX CPU, running two Demucs passes concurrently would thrash anyway, so
serializing here fixes both the hang and the resource contention in one move: only
one separation runs at a time, others block until it's done."""
key = _file_hash(file_path)
cache_dir = os.path.join(STEM_CACHE_DIR, key)
stem_paths = {name: os.path.join(cache_dir, f"{name}.wav") for name in STEM_NAMES}
......@@ -52,9 +73,8 @@ def separate_stems(file_path: str) -> dict:
os.makedirs(cache_dir, exist_ok=True)
from demucs.api import Separator
separator = Separator(model="htdemucs")
with _separator_lock:
separator = _get_separator()
_, separated = separator.separate_audio_file(file_path)
for name, tensor in separated.items():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment