mlx-audio icon indicating copy to clipboard operation
mlx-audio copied to clipboard

Semaphore Leak Warning on Python 3.13.7 with Kokoro TTS in Async Context

Open Mohammed-Yasin-Mulla opened this issue 4 months ago • 3 comments

Environment

  • mlx-audio: v0.2.5
  • Model: mlx-community/Kokoro-82M-bf16
  • Python: 3.13.7
  • Machine: MacBook Pro M3 Pro (18GB RAM)
  • OS: macOS 26.0.1

Issue

Getting persistent semaphore leak warnings from Python's resource_tracker when using mlx-audio for real-time TTS:

INFO:whisperlivekit.audio_processor:internal_buffer=0.00s | lag=0.29s |
/Users/mohammedyasinmulla/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/multiprocessing/resource_tracker.py:324: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown: {'/loky-12097-8myixz42'}

When it occurs:

  • At first audio generation (client connection)

Attempted Fixes

  1. Set LOKY_MAX_CPU_COUNT=1 → Reduced warnings but still appear occasionally
  2. Explicit loky cleanup:
    from joblib.externals.loky import get_reusable_executor
    executor = get_reusable_executor(max_workers=None, timeout=1)
    executor.shutdown(wait=True, kill_workers=True)
    
  3. Aggressive GC → Minimal impact
  4. Force CPU mode (disabled MPS) → Improved stability, warnings persist

Questions

  1. Does mlx-audio use joblib internally? (I see it's in dependencies)
  2. Is there a recommended async pattern for long-running services?
  3. Are there known Python 3.13.7 compatibility issues?
  4. Does model.generate() spawn background processes?
  5. Proper cleanup procedure for MLX models in async context?

Related:

  • Also using mlx-whisper (via WhisperLiveKit) in same process
  • Both may involve joblib/loky → cumulative effect?

Mohammed-Yasin-Mulla avatar Oct 20 '25 10:10 Mohammed-Yasin-Mulla

Hey @Mohammed-Yasin-Mulla

Could you provide a reproducible example?

Also, we have whisper support in mlx-audio.

Finally, leaked semaphore means that a process is consuming all your memory, maybe it's mlx-whisper + whisperLivekit combo.

Blaizzy avatar Oct 21 '25 21:10 Blaizzy

Hey @Blaizzy Please check my repo: https://github.com/Mohammed-Yasin-Mulla/Local-Voice-AI/tree/mlx-audio In the mlx-audio branch

This sometimes randomly works out of the blue, and most of the time gives me this error above.

Mohammed-Yasin-Mulla avatar Oct 22 '25 04:10 Mohammed-Yasin-Mulla

Hey @Mohammed-Yasin-Mulla

This code base is vast, it would help and be much faster if you could pinpoint the issue for me to investigate.

I don’t know where to start as I’m not familiar with it.

Blaizzy avatar Oct 28 '25 08:10 Blaizzy