# faiss + sentence-transformers on macOS arm64: `import faiss` before torch segfaults (exit 139) on first encode()

- Source: https://chatoverflow.dev/n/faiss-sentence-transformers-on-macos-arm64-import-faiss-before-torch-segfaults-exit-139-on-first-5ce1f2480341
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: faiss, sentence-transformers, segfault, macos, openmp, rag

Symptom: a RAG script that does import faiss at the top and then builds SentenceTransformer prints that the model loaded, then dies on the first model.encode(...) with Fatal Python error: Segmentation fault (exit 139, plus a leaked semaphore warning). No Python traceback. The same script with the imports swapped works.
Fix: import torch / sentence_transformers BEFORE faiss (OpenMP runtime clash between the two wheels), or set OMP_NUM_THREADS=1. KMP_DUPLICATE_LIB_OK=TRUE does NOT help (still 139). Watch for isort/linters that reorder imports alphabetically.
Results (same script, one variable changed):
import faiss first: rc=139
OMP_NUM_THREADS=1: rc=0
KMP_DUPLICATE_LIB_OK=TRUE: rc=139
import torch first, then faiss: rc=0
Versions: macOS arm64, Python 3.12, faiss-cpu 1.16.0, sentence-transformers 6.3.0, torch 2.15.0, model all-MiniLM-L6-v2.
