From 5ba251e192e6be62ddf11fe9268efa1f507e1d27 Mon Sep 17 00:00:00 2001 From: Helen Chen Date: Mon, 13 Jul 2026 12:44:07 -0400 Subject: [PATCH] Save socket fd to be cleaned up in functional test Functional tests that create a netlink dispatcher thread should close the netlink socket and terminate the netlink dispatcher thread. This requires the dispatcher thread to store the netlink socket's socket FD, such that a functional test case can close the socket to trigger the existing code to terminate the dispatcher thread. Related-Bug: #2144617 Assisted-By: Claude Opus 4.6 Change-Id: I6e3ae037a8575be137bad65f2fd0cf4c719033b3 Signed-off-by: Helen Chen --- neutron/agent/linux/nl_dispatcher.py | 3 +++ .../functional/agent/ovn/extensions/test_evpn.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/neutron/agent/linux/nl_dispatcher.py b/neutron/agent/linux/nl_dispatcher.py index a12de8612e7..bf2609e2e2a 100644 --- a/neutron/agent/linux/nl_dispatcher.py +++ b/neutron/agent/linux/nl_dispatcher.py @@ -19,6 +19,7 @@ import logging import os import threading import time +import weakref from oslo_log import log from pyroute2 import iproute @@ -43,6 +44,7 @@ class NetlinkDispatcher: self._groups = groups self._replay_start_callbacks = [] self._replay_end_callbacks = [] + self._ipr = lambda: None def register_handler(self, event_type, handler): """Register a handler for a specific netlink message type. @@ -75,6 +77,7 @@ class NetlinkDispatcher: def _sock(self): ipr = iproute.IPRoute() ipr.bind(self._groups) + self._ipr = weakref.ref(ipr) try: yield ipr finally: diff --git a/neutron/tests/functional/agent/ovn/extensions/test_evpn.py b/neutron/tests/functional/agent/ovn/extensions/test_evpn.py index 22da0f45f83..09871afb03f 100644 --- a/neutron/tests/functional/agent/ovn/extensions/test_evpn.py +++ b/neutron/tests/functional/agent/ovn/extensions/test_evpn.py @@ -17,6 +17,7 @@ import socket from unittest import mock from oslo_utils import uuidutils +from pyroute2 import iproute from pyroute2.netlink import rtnl from neutron.agent.linux import ip_lib @@ -44,6 +45,20 @@ class TestVrfHandlerLifecycle(functional_base.BaseSudoTestCase): except Exception: pass + def _stop_dispatcher(self, dispatcher): + dispatcher.RETRY_BACKOFF = 0 + with mock.patch.object(nl_dispatcher.os, '_exit', + side_effect=SystemExit): + with mock.patch.object(iproute, 'IPRoute', + side_effect=RuntimeError): + ipr = dispatcher._ipr() + if ipr: + ipr.close() + try: + dispatcher._thread.join(timeout=5) + except SystemExit: + pass + def test_vrf_handler_lifecycle(self): vrf_handler = netlink_monitor.VrfHandler(fsm.EvpnFSM()) @@ -63,6 +78,7 @@ class TestVrfHandlerLifecycle(functional_base.BaseSudoTestCase): self.addCleanup(self._safe_delete, preexisting_vrf) dispatcher.start() + self.addCleanup(self._stop_dispatcher, dispatcher) utils.wait_until_true( lambda: preexisting_vrf in vrf_handler._known_vrfs, timeout=10, sleep=0.1)