Skip to content

[MP] Fix GIL and CUDA driver lock order inversion deadlock in store/retrieve#2823

Closed
maobaolong wants to merge 0 commit intoLMCache:devfrom
maobaolong:fix_store_dl2.gh
Closed

[MP] Fix GIL and CUDA driver lock order inversion deadlock in store/retrieve#2823
maobaolong wants to merge 0 commit intoLMCache:devfrom
maobaolong:fix_store_dl2.gh

Conversation

@maobaolong
Copy link
Copy Markdown
Collaborator

@maobaolong maobaolong commented Mar 19, 2026

Problem

Under concurrent store workloads, the LMCache MP server can deadlock due to a GIL ↔ CUDA driver lock order inversion between get_slot_mapping_tensor() and multi_layer_kv_transfer().

  • Thread A holds transfer_lock, calls multi_layer_kv_transfer (pybind11 gil_scoped_release), finishes the CUDA kernel, then tries to re-acquire GIL in ~gil_scoped_release.
  • Thread B holds GIL, calls get_slot_mapping_tensorflatten() → PyTorch C++ dispatch, which tries to acquire the CUDA driver lock.

This AB-BA deadlock freezes the entire ThreadPoolExecutor, causing PING heartbeat timeouts and vLLM entering degraded/unhealthy mode.

Root Cause

get_slot_mapping_tensor() was called outside transfer_lock, while multi_layer_kv_transfer() was called inside it. Two different lock acquisition orders (GIL→CUDA vs CUDA→GIL) across threads created a classic lock order inversion.

Fix

Widen transfer_lock scope in both store() and retrieve() to cover get_slot_mapping_tensor() alongside multi_layer_kv_transfer(), ensuring both operations are serialized under the same lock and eliminating the AB-BA deadlock.

  • store: Lift transfer_lock from per-chunk to per-request, covering get_slot_mapping_tensor + the entire transfer loop.
  • retrieve: Move get_slot_mapping_tensor inside transfer_lock, wrapping it together with _retrieve_loop.

Performance Impact

Negligible — transfer_lock is per-device and CUDA stream execution is inherently serial. The lock scope change only affects the Python-level scheduling, not actual GPU parallelism.

Stack while deadlock


py-spy dump -p 123088 --native
Process 123088: /usr/bin/python3 -m lmcache.v1.multiprocess.server --host localhost --port 15556 --chunk-size 512 --l1-size-gb 500 --eviction-policy LRU --max-workers 16 --no-l1-use-lazy --no-l1-contiguous-alloc
Python v3.12.12 (/usr/bin/python3.12)

Thread 123088 (idle): "MainThread"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    run_cache_server (lmcache/v1/multiprocess/server.py:976)
    <module> (lmcache/v1/multiprocess/server.py:1002)
    _run_code (<frozen runpy>:88)
    _run_module_as_main (<frozen runpy>:198)
    0x7f661b3c1d90 (libc.so.6)
Thread 123166 (idle): "Thread-1 (serve_forever)"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    select (selectors.py:415)
    serve_forever (socketserver.py:235)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124265 (idle): "Thread-8 (_eviction_loop)"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    _eviction_loop (storage_controllers/eviction_controller.py:67)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124266 (idle): "Thread-9 (_store_loop)"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    _store_loop (storage_controllers/store_controller.py:252)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124267 (idle): "Thread-10 (_prefetch_loop)"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    _prefetch_loop (storage_controllers/prefetch_controller.py:586)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124442 (idle): "mq-server-thread"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    0x7f6619b666f3 (zmq/backend/cython/_zmq.abi3.so)
    poll (zmq/sugar/poll.py:106)
    _main_loop (lmcache/v1/multiprocess/mq.py:444)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124443 (idle): "PrometheusController"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    wait (threading.py:359)
    wait (threading.py:655)
    _run (prometheus_controller.py:68)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126879 (idle): "ThreadPoolExecutor-1_0"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    pybind11::gil_scoped_acquire::gil_scoped_acquire (libtorch_python.so)
    torch::detail::(anonymous namespace)::ConcretePyInterpreterVTable::decref (libtorch_python.so)
    c10::TensorImpl::decref_pyobject (libc10.so)
    torch::autograd::VariableType::(anonymous namespace)::view (libtorch_cpu.so)
    c10::impl::wrap_kernel_functor_unboxed_<c10::impl::detail::WrapFunctionIntoFunctor_<c10::CompileTimeFunctionPointer<at::Tensor(c10::DispatchKeySet, at::Tensor const&, c10::ArrayRef<c10::SymInt>), &torch::autograd::VariableType::(anonymous namespace)::view(c10::DispatchKeySet, at::Tensor const&, c10::ArrayRef<c10::SymInt>)>, at::Tensor, c10::guts::typelist::typelist<c10::DispatchKeySet, at::Tensor const&, c10::ArrayRef<c10::SymInt> > >, at::Tensor(c10::DispatchKeySet, at::Tensor const&, c10::ArrayRef<c10::SymInt>)>::call (libtorch_cpu.so)
    at::_ops::view::call (libtorch_cpu.so)
    at::native::reshape_symint (libtorch_cpu.so)
    at::native::flatten (libtorch_cpu.so)
    c10::impl::wrap_kernel_functor_unboxed_<c10::impl::detail::WrapFunctionIntoFunctor_<c10::CompileTimeFunctionPointer<at::Tensor(at::Tensor const&, long, long), &at::(anonymous namespace)::(anonymous namespace)::wrapper_CompositeImplicitAutograd_using_ints_flatten(at::Tensor const&, long, long)>, at::Tensor, c10::guts::typelist::typelist<at::Tensor const&, long, long> >, at::Tensor(at::Tensor const&, long, long)>::call (libtorch_cpu.so)
    at::_ops::flatten_using_ints::call (libtorch_cpu.so)
    torch::autograd::THPVariable_flatten (libtorch_python.so)
    get_slot_mapping_tensor (lmcache/v1/multiprocess/gpu_context.py:205)
    store (lmcache/v1/multiprocess/server.py:300)
    run (concurrent/futures/thread.py:59)
    _worker (concurrent/futures/thread.py:93)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126881 (idle): "ThreadPoolExecutor-1_1"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126883 (idle): "ThreadPoolExecutor-1_2"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126884 (idle): "ThreadPoolExecutor-1_3"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126887 (idle): "ThreadPoolExecutor-1_4"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126888 (idle): "ThreadPoolExecutor-1_5"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126890 (idle): "ThreadPoolExecutor-1_6"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    torch::autograd::dispatch_to (libtorch_python.so)
    torch::autograd::THPVariable_to (libtorch_python.so)
    list_to_gpu_tensor (lmcache/v1/multiprocess/gpu_context.py:46)
    get_slot_mapping_tensor (lmcache/v1/multiprocess/gpu_context.py:204)
    store (lmcache/v1/multiprocess/server.py:300)
    run (concurrent/futures/thread.py:59)
    _worker (concurrent/futures/thread.py:93)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126892 (idle): "ThreadPoolExecutor-1_7"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    torch::autograd::dispatch_to (libtorch_python.so)
    torch::autograd::THPVariable_to (libtorch_python.so)
    list_to_gpu_tensor (lmcache/v1/multiprocess/gpu_context.py:46)
    get_slot_mapping_tensor (lmcache/v1/multiprocess/gpu_context.py:204)
    store (lmcache/v1/multiprocess/server.py:300)
    run (concurrent/futures/thread.py:59)
    _worker (concurrent/futures/thread.py:93)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126894 (idle): "ThreadPoolExecutor-1_8"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126896 (idle): "ThreadPoolExecutor-1_9"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126897 (idle): "ThreadPoolExecutor-1_10"
    0x7f661b4292c0 (libc.so.6)
    pthread_mutex_lock (libc.so.6)
    0x7f661a041dd4 (libcudart.so.12)
    0x7f661a04408b (libcudart.so.12)
    0x7f661a03c431 (libcudart.so.12)
    0x7f661a01118b (libcudart.so.12)
    cudaSetDevice (libcudart.so.12)
    c10::cuda::ExchangeDevice (libc10_cuda.so)
    THCPModule_exchangeDevice (libtorch_python.so)
    __enter__ (torch/cuda/__init__.py:550)
    store (lmcache/v1/multiprocess/server.py:296)
    run (concurrent/futures/thread.py:59)
    _worker (concurrent/futures/thread.py:93)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126899 (idle): "ThreadPoolExecutor-1_11"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126901 (idle): "ThreadPoolExecutor-1_12"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    0x7f661a4f06ca (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126902 (idle): "ThreadPoolExecutor-1_13"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126905 (active): "ThreadPoolExecutor-1_14"
    0x7f65dbc41c90 (libcuda.so.575.57.08)
    0x7f65db9f9493 (libcuda.so.575.57.08)
    0x7f65dba3a2eb (libcuda.so.575.57.08)
    0x7f65dc6cc998 (libcuda.so.575.57.08)
    0x7f65dc6ccdc8 (libcuda.so.575.57.08)
    0x7f65dba0542c (libcuda.so.575.57.08)
    0x7f65dc6cb05d (libcuda.so.575.57.08)
    0x7f65dbacc0cf (libcuda.so.575.57.08)
    0x7f65dba96528 (libcuda.so.575.57.08)
    0x7f65dba967a5 (libcuda.so.575.57.08)
    0x7f65dbb0dc0f (libcuda.so.575.57.08)
    cuLibraryGetModule (libcuda.so.575.57.08)
    0x7f661a03d10a (libcudart.so.12)
    0x7f661a03d8a8 (libcudart.so.12)
    0x7f661a03d936 (libcudart.so.12)
    0x7f661a04086a (libcudart.so.12)
    0x7f661a0141a1 (libcudart.so.12)
    cudaLaunchKernel (libcudart.so.12)
    cudaLaunchKernel<char> (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    __device_stub__ZN3lmc35load_and_reshape_multi_layer_kernelIlLb1EL11GPUKVFormat3EEEvPT_PS3_PKliiiiii (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    lmc::__wrapper__device_stub_load_and_reshape_multi_layer_kernel<long, true, (GPUKVFormat)3> (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    lmc::load_and_reshape_multi_layer_kernel<long, true, (GPUKVFormat)3> (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    multi_layer_kv_transfer_templated<long> (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    multi_layer_kv_transfer (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    pybind11::gil_scoped_release::~gil_scoped_release (gil.h:180)
    pybind11::detail::argument_loader<at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int>::call<void, pybind11::gil_scoped_release, void (*&)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int)> && (cast.h:2112)
    pybind11::cpp_function::initialize<void (*&)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int), void, at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int, pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg_v, pybind11::arg_v, pybind11::call_guard<pybind11::gil_scoped_release> >(void (*&)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int), void (*)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int), pybind11::name const, pybind11::scope&, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg_v, pybind11::arg_v, pybind11::call_guard<pybind11::gil_scoped_release>)::{lambda(pybind11::detail::function_call&)#3}::operator() const (pybind11.h:431)
    pybind11::cpp_function::initialize<void (*&)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int), void, at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int, pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg_v, pybind11::arg_v, pybind11::call_guard<pybind11::gil_scoped_release> >(void (*&)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int), void (*)(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Device const&, int, TransferDirection, GPUKVFormat, int, int), pybind11::name const, pybind11::scope&, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg, pybind11::arg_v, pybind11::arg_v, pybind11::call_guard<pybind11::gil_scoped_release>)::{lambda(pybind11::detail::function_call&)#3}::_FUN (pybind11.h:401)
    pybind11::cpp_function::dispatcher (lmcache/c_ops.cpython-312-x86_64-linux-gnu.so)
    store (lmcache/v1/multiprocess/server.py:344)
    run (concurrent/futures/thread.py:59)
    _worker (concurrent/futures/thread.py:93)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 126907 (idle): "ThreadPoolExecutor-1_15"
    0x7f661b429117 (libc.so.6)
    0x7f661b434c78 (libc.so.6)
    PyThread_acquire_lock_timed (python3.12)
    0x7f661a4f06bd (_queue.cpython-312-x86_64-linux-gnu.so)
    0x7f661a4f09e4 (_queue.cpython-312-x86_64-linux-gnu.so)
    _worker (concurrent/futures/thread.py:90)
    run (threading.py:1012)
    _bootstrap_inner (threading.py:1075)
    _bootstrap (threading.py:1032)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124726 (idle): "Dummy-19"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124734 (idle): "Dummy-20"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124743 (idle): "Dummy-22"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124720 (idle): "Dummy-18"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124697 (idle): "Dummy-14"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124703 (idle): "Dummy-15"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124688 (idle): "Dummy-12"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)
Thread 124711 (idle): "Dummy-16"
    0x7f661b429117 (libc.so.6)
    pthread_cond_timedwait (libc.so.6)
    runtime__HostFnFunc (runtime.cpp:24659)
    0x7f65dbac4233 (libcuda.so.575.57.08)
    0x7f65dbad4123 (libcuda.so.575.57.08)
    0x7f661b42cac3 (libc.so.6)
    0x7f661b4be8d0 (libc.so.6)

Note

Low Risk
The provided diff contains no code changes, so there is no functional or behavioral risk to review.

Overview
No changes are included in the provided git diff (+++ /dev/null only), so there is nothing to review in this PR as-is.

Written by Cursor Bugbot for commit ba3ba51. This will update automatically on new commits. Configure here.

@maobaolong maobaolong requested a review from ApostaC March 19, 2026 13:14
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical deadlock condition that arose from an inverted lock order between Python's Global Interpreter Lock (GIL) and CUDA driver locks during data transfer operations. By carefully adjusting the scope of the transfer_lock and reordering specific function calls, the changes ensure that concurrent operations involving GPU context and memory transfers acquire locks in a consistent and safe sequence, thereby preventing the deadlock.

Highlights

  • Deadlock Prevention: Addressed a GIL and CUDA driver lock order inversion deadlock that could occur during store and retrieve operations by reordering lock acquisition.
  • Lock Scope Adjustment: Modified the scope of gpu_context.transfer_lock to encompass the entire batch processing for both store and _retrieve_loop functions, ensuring get_slot_mapping_tensor and multi_layer_kv_transfer do not run concurrently in a way that causes deadlocks.
  • Function Call Reordering: Reordered calls to get_slot_mapping_tensor and lmcache_memcpy_async_h2d relative to the transfer_lock to ensure proper and safe lock acquisition order.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses a potential deadlock issue between the Python GIL and CUDA driver locks in the store and retrieve functions. By introducing a transfer_lock, it ensures that get_slot_mapping_tensor and multi_layer_kv_transfer are serialized, preventing lock order inversion. The fix is sound. I have one suggestion for the retrieve function to further improve performance by reducing the scope of the lock.

Comment thread lmcache/v1/multiprocess/server.py Outdated
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.

1 participant