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)