Skip to content

Commit 2bfdbf2

Browse files
authored
[V1][Core] Use weakref.finalize instead of atexit (vllm-project#11242)
Signed-off-by: Tyler Michael Smith <tyler@neuralmagic.com>
1 parent e88db68 commit 2bfdbf2

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

vllm/v1/engine/core_client.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import atexit
21
import os
2+
import weakref
33
from typing import List, Optional
44

55
import msgspec
@@ -165,15 +165,9 @@ def __init__(
165165
ready_path=ready_path, # type: ignore[misc]
166166
**kwargs,
167167
)
168-
atexit.register(self.shutdown)
168+
self._finalizer = weakref.finalize(self, self.shutdown)
169169

170170
def shutdown(self):
171-
# During final garbage collection in process shutdown, atexit may be
172-
# None.
173-
if atexit:
174-
# in case shutdown gets called via __del__ first
175-
atexit.unregister(self.shutdown)
176-
177171
# Shut down the zmq context.
178172
self.ctx.destroy(linger=0)
179173

@@ -197,9 +191,6 @@ def shutdown(self):
197191
os.remove(socket_file)
198192
self.proc_handle = None
199193

200-
def __del__(self):
201-
self.shutdown()
202-
203194

204195
class SyncMPClient(MPClient):
205196
"""Synchronous client for multi-proc EngineCore."""

vllm/v1/executor/multiproc_executor.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import atexit
21
import os
32
import pickle
43
import signal
54
import sys
65
import time
6+
import weakref
77
from dataclasses import dataclass
88
from enum import Enum, auto
99
from multiprocessing.process import BaseProcess
@@ -37,7 +37,7 @@ class MultiprocExecutor(Executor):
3737
def __init__(self, vllm_config: VllmConfig) -> None:
3838
# Call self.shutdown at exit to clean up
3939
# and ensure workers will be terminated.
40-
atexit.register(self.shutdown)
40+
self._finalizer = weakref.finalize(self, self.shutdown)
4141

4242
self.vllm_config = vllm_config
4343
self.parallel_config = vllm_config.parallel_config
@@ -195,14 +195,10 @@ def _cleanup_sockets(self):
195195
os.remove(socket_path)
196196

197197
def shutdown(self):
198-
if atexit:
199-
# in case shutdown was called explicitly, we don't need to call it
200-
# again
201-
atexit.unregister(self.shutdown)
202198
"""Properly shut down the executor and its workers"""
203199
if getattr(self, 'shutting_down', False):
204200
self.shutting_down = True
205-
for w in self.workers: #TODO: not sure if needed
201+
for w in self.workers:
206202
w.worker_response_mq = None
207203
self._ensure_worker_termination()
208204

0 commit comments

Comments
 (0)