fix: set onnxruntime thread options to avoid LXC pthread_setaffinity_np errors

When running inside LXC containers (e.g. Proxmox), onnxruntime's thread pool
tries to call pthread_setaffinity_np() which fails with EINVAL (error 22).
Setting intra/inter_op_num_threads=1 when OMP_NUM_THREADS=1 is set prevents
this error and stabilizes ML inference (face_recognition, semantic_search,
classification) in containerized environments.

Document OMP_NUM_THREADS=1 in docker-compose.yml as a commented option for
LXC deployments.

Fixes #22620
This commit is contained in:
Arturo Naredo 2026-03-25 05:12:26 +01:00
parent 416a9b7692
commit 749d749334
2 changed files with 22 additions and 5 deletions

View File

@ -24,6 +24,10 @@ services:
# capabilities: [gpu]
environment:
YOLO_MODELS: ""
# Set OMP_NUM_THREADS=1 when running in LXC containers (e.g. Proxmox) to prevent
# onnxruntime from calling pthread_setaffinity_np(), which fails in LXC with EINVAL.
# This affects face_recognition, semantic_search, and classification features.
# OMP_NUM_THREADS: "1"
# devices:
# - /dev/bus/usb:/dev/bus/usb # Uncomment for Google Coral USB
# - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware

View File

@ -24,23 +24,36 @@ def is_arm64_platform() -> bool:
def get_ort_session_options(
is_complex_model: bool = False,
) -> ort.SessionOptions | None:
) -> ort.SessionOptions:
"""Get ONNX Runtime session options with appropriate settings.
Args:
is_complex_model: Whether the model needs basic optimization to avoid graph fusion issues.
Returns:
SessionOptions with appropriate optimization level, or None for default settings.
SessionOptions with appropriate optimization level and thread settings.
"""
sess_options = ort.SessionOptions()
# When OMP_NUM_THREADS=1 is set (recommended for LXC/container environments),
# or when running in a container where pthread_setaffinity_np is blocked,
# onnxruntime logs repeated errors like:
# "pthread_setaffinity_np failed ... error code: 22 ... Invalid argument"
# Setting intra/inter_op_num_threads=1 prevents the thread pool from trying
# to set CPU affinity, eliminating the error spam in LXC containers (Proxmox, etc.).
import os
omp_threads = int(os.environ.get("OMP_NUM_THREADS", "0"))
if omp_threads == 1:
sess_options.intra_op_num_threads = 1
sess_options.inter_op_num_threads = 1
if is_complex_model:
sess_options = ort.SessionOptions()
sess_options.graph_optimization_level = (
ort.GraphOptimizationLevel.ORT_ENABLE_BASIC
)
return sess_options
return None
return sess_options
# Import OpenVINO only when needed to avoid circular dependencies