I'm encountering a persistent issue where importing the sentence_transformers library on macOS causes my Python script to hang indefinitely. When the script hangs, the only output I see is a low-level mutex lock error. The script becomes unkillable (Ctrl+C does not work), and I have to force-quit the terminal or process.
The error message is:
[mutex.cc : 452] RAW: Lock blocking 0x11b3e1888 @
This problem began after I was experimenting with running sentence-transformers tasks concurrently using Python's asyncio library. My code structure was similar to this:
import asyncio
# from sentence_transformers import SentenceTransformer # This import is now problematic
# model = SentenceTransformer('all-MiniLM-L6-v2') # Example model loading
async def process_with_tracking(org_data):
# Some processing logic that would eventually use the model
# e.g., model.encode(org_data['text'])
pass
async def main():
orgs = [...] # A list of data objects
tasks = []
# Create tasks for each org
for org in orgs:
task = asyncio.create_task(process_with_tracking(org))
tasks.append(task)
# Wait for all tasks to complete
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
# asyncio.run(main())
My hypothesis is that a previous concurrent execution created a deadlock or corrupted a shared resource/cache file used by sentence-transformers or one of its dependencies (like Hugging Face Transformers or PyTorch).
The issue is now system-wide and persists even when I am not using asyncio or any form of concurrency. A minimal script with just the import statement is enough to trigger the hang:
test_import.py
print("Attempting to import sentence_transformers...")
import sentence_transformers
print("Import successful!") # This line is never reached
When I run python test_import.py, the script hangs after the first print statement, and I see the [mutex.cc] error.
pip uninstall sentence-transformers and pip install sentence-transformers multiple times.asyncio code as the immediate trigger.sentence-transformers or Hugging Face transformers store cache, configuration, or lock files on macOS that I could manually inspect or delete? (e.g., ~/.cache/huggingface/)tokenizers) on macOS that could lead to this state? How can I reset it?I found that downgrading Python from 3.13 to 3.12.7 worked for me to get the sentence_transformer library to run without the multi-threading issues. It must be a bug with the newer release of Python.