Skip to content

Commit 1cc496f

Browse files
committed
[P/D] Support disconnecting from clean shutdown
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
1 parent ebf7605 commit 1cc496f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

vllm/distributed/kv_transfer/kv_connector/v1/nixl_connector.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3+
import atexit
34
import contextlib
45
import logging
56
import math
@@ -553,8 +554,31 @@ def __init__(self, vllm_config: VllmConfig, engine_id: str):
553554
# finish reading before safely freeing the blocks.
554555
self.consumer_notification_counts_by_req = defaultdict[ReqId, int](int)
555556

557+
self._shutdown_lock = threading.Lock()
558+
self._shutdown_called = False
559+
atexit.register(self._shutdown)
560+
556561
def __del__(self):
557562
"""Cleanup background threads on destruction."""
563+
self._shutdown()
564+
565+
def _shutdown(self):
566+
"""Cleanup resources on shutdown."""
567+
# It's possible that __init__ fails and these attributes are not set.
568+
if not hasattr(self, "_shutdown_lock"):
569+
return
570+
with self._shutdown_lock:
571+
if getattr(self, "_shutdown_called", False):
572+
return
573+
self._shutdown_called = True
574+
self.nixl_wrapper.release_dlist_handle(self.src_xfer_side_handle)
575+
for dst_xfer_side_handle in self.dst_xfer_side_handles.values():
576+
self.nixl_wrapper.release_dlist_handle(dst_xfer_side_handle)
577+
for remote_agents in self._remote_agents.values():
578+
for agent_name in remote_agents.values():
579+
self.nixl_wrapper.remove_remote_agent(agent_name)
580+
for desc in self._registered_descs:
581+
self.nixl_wrapper.deregister_memory(desc)
558582
self._handshake_initiation_executor.shutdown(wait=False)
559583
if self._nixl_handshake_listener_t:
560584
self._nixl_handshake_listener_t.join(timeout=0)

0 commit comments

Comments
 (0)