[Bugfix][Security] Fix concurrent sparse invariant race bypassing CVE remediation (#48583)

Signed-off-by: jperezde <jperezde@redhat.com>
This commit is contained in:
Juan Pérez de Algaba
2026-07-14 11:08:24 +00:00
committed by GitHub
parent 50ac1c7bab
commit 793cf79c89
5 changed files with 298 additions and 13 deletions
+27
View File
@@ -0,0 +1,27 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""
Thread-safe wrapper for sparse tensor invariant validation.
PyTorch's `torch.sparse.check_sparse_tensor_invariants()` context manager
manipulates a **process-global** flag (save/enable/restore). When multiple
embedding-load operations run concurrently on a thread-pool executor, one
context can restore the flag to `False` while another thread is still inside
its guard, bypassing the invariant check.
All call sites MUST use `check_sparse_tensor_invariants_threadsafe()`
which serializes access behind a lock.
"""
import contextlib
import threading
import torch
_SPARSE_LOAD_LOCK = threading.Lock()
@contextlib.contextmanager
def check_sparse_tensor_invariants_threadsafe():
with _SPARSE_LOAD_LOCK, torch.sparse.check_sparse_tensor_invariants():
yield