Skip to content

fix: cap BLAS/OpenMP threads — concurrency fix (#316)#359

Merged
buildingjoshbetter merged 1 commit into
mainfrom
fix/s01-blas-thread-cap
May 21, 2026
Merged

fix: cap BLAS/OpenMP threads — concurrency fix (#316)#359
buildingjoshbetter merged 1 commit into
mainfrom
fix/s01-blas-thread-cap

Conversation

@buildingjoshbetter
Copy link
Copy Markdown
Owner

Summary

  • Sets OMP_NUM_THREADS=1, MKL_NUM_THREADS=1, OPENBLAS_NUM_THREADS=1, NUMEXPR_MAX_THREADS=1 via setdefault in both model_server.py and mcp_server.py
  • Placed before any torch/numpy imports
  • Uses setdefault so user overrides are respected

What this fixes

N concurrent MCP processes × N BLAS threads = N² context switches. With 6 sessions: 60 threads fighting for 10 cores. Measured 200,000x slowdown with 19-minute hangs on m.add().

Test evidence

  • Custom validation tests: 3/3 passed (thread caps set, source verified, user overrides respected)
  • Sacred parameters: verified unchanged (MPS watermark, FTS5 tokenizer, vector dims all intact)
  • Only 2 files changed, 9 insertions, 1 deletion

Risk

Single-threaded BLAS is slightly slower for large batch operations (tier switch vector rebuild). Negligible for normal single-query inference.

Closes #316

Set OMP_NUM_THREADS, MKL_NUM_THREADS, OPENBLAS_NUM_THREADS, and
NUMEXPR_MAX_THREADS to "1" via setdefault in both model_server.py
and mcp_server.py before any model imports.

Without this, N concurrent MCP processes each spawn N BLAS threads,
causing N-squared context switch collapse (measured 19-minute hangs).
Using setdefault so users can override if needed.

Closes #316
@buildingjoshbetter buildingjoshbetter merged commit 20a024e into main May 21, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BLAS/OpenMP thread oversubscription causes multi-minute m.add() hangs under concurrent MCP procs

1 participant