[Bugfix] Close MessageQueue zmq resources via finalizer to avoid exit (#60472)

Signed-off-by: aarushjain29 <aarushi.jain2@amd.com>
Co-authored-by: Andreas Karatzas <akaratza@amd.com>
This commit is contained in:
Aarushi Jain
2026-10-08 13:53:00 -05:00
committed by GitHub
co-authored by Andreas Karatzas
parent 56e6da2c47
commit 9bbc581560
2 changed files with 47 additions and 0 deletions
+22
View File
@@ -1,11 +1,13 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import gc
import io
import pickle
import random
import threading
import time
import weakref
from types import SimpleNamespace
from unittest import mock
@@ -634,6 +636,26 @@ def test_acquire_read_releases_slot_when_reader_raises():
reader.shutdown()
def test_reader_in_reference_cycle_is_collected_without_blocking():
writer = MessageQueue(
n_reader=1,
n_local_reader=1,
max_chunk_bytes=1024 * 1024,
max_chunks=1,
)
reader = MessageQueue.create_from_handle(writer.export_handle(), rank=0)
reader.cycle = reader
reader_ref = weakref.ref(reader)
del reader
collector = threading.Thread(target=gc.collect, daemon=True)
collector.start()
collector.join(timeout=10)
assert not collector.is_alive()
assert reader_ref() is None
def test_warning_logs(caplog_vllm):
"""Test that warning logs are emitted at VLLM_RINGBUFFER_WARNING_INTERVAL intervals
when indefinite=False, and are not emitted when indefinite=True.
@@ -9,6 +9,7 @@ import shutil
import sys
import threading
import time
import weakref
from contextlib import contextmanager
from dataclasses import dataclass, field
from multiprocessing import shared_memory
@@ -472,6 +473,12 @@ class Handle:
remote_addr_ipv6: bool = False
def _close_zmq(context: zmq.Context, sockets: list[zmq.Socket]) -> None:
for socket in sockets:
socket.close(linger=0)
context.term()
class MessageQueue:
def __init__(
self,
@@ -561,9 +568,26 @@ class MessageQueue:
remote_subscribe_addr=remote_subscribe_addr,
remote_addr_ipv6=remote_addr_ipv6,
)
self._close_zmq_on_collect(context)
logger.debug("vLLM message queue communication handle: %s", self.handle)
def _close_zmq_on_collect(self, context: zmq.Context) -> None:
# pyzmq's Context.__del__ can block forever in term() when the context
# and its sockets are collected in the same GC cycle, so the finalizer
# keeps them alive and closes the sockets before terminating.
sockets = [self.local_socket, self.remote_socket]
if self._spin_condition is not None:
sockets += [
self._spin_condition.local_notify_socket,
self._spin_condition.read_cancel_socket,
self._spin_condition.write_cancel_socket,
]
finalizer = weakref.finalize(
self, _close_zmq, context, [s for s in sockets if s is not None]
)
finalizer.atexit = False # type: ignore[misc]
def export_handle(self) -> Handle:
return self.handle
@@ -613,6 +637,7 @@ class MessageQueue:
self._spin_condition = None # type: ignore
self.shutting_down = False
self._close_zmq_on_collect(context)
return self
def wait_until_ready(self):