From eb4fd8b608dd13857cb53bb3760496a5d5caf300 Mon Sep 17 00:00:00 2001 From: gchai Date: Tue, 1 Dec 2026 18:02:10 -0500 Subject: [PATCH 1/2] Close stream interfaces leaked by three unit tests - test_tunnel_tunnel_arg, test_TCPInterface_writeBytes_reconnects and test_Tunnel_on_non_linux_system left reader threads spinning on MagicMocks, growing memory until the 3.14/3.15 CI runners were killed - Add an autouse conftest guard that fails any test leaving a 'stream reader' thread alive --- meshtastic/tests/conftest.py | 14 +++++++++++++ meshtastic/tests/test_main.py | 27 ++++++++++++++------------ meshtastic/tests/test_tcp_interface.py | 12 ++++++++---- meshtastic/tests/test_tunnel.py | 9 ++++++--- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/meshtastic/tests/conftest.py b/meshtastic/tests/conftest.py index c863888a7..7ed92a75a 100644 --- a/meshtastic/tests/conftest.py +++ b/meshtastic/tests/conftest.py @@ -1,6 +1,7 @@ """Common pytest code (place for fixtures).""" import argparse +import threading from unittest.mock import MagicMock import pytest @@ -17,6 +18,19 @@ ) from .fw_helpers import set_region + +@pytest.fixture(autouse=True) +def no_leaked_stream_readers(): + """Fail a test that leaves a StreamInterface reader thread running. + + Over a mocked transport the reader spins without sleeping and every MagicMock call is recorded, + so a leaked reader grows memory for the rest of the session until the CI runner is killed.""" + before = {t.ident for t in threading.enumerate() if t.name == "stream reader"} + yield + leaked = [t for t in threading.enumerate() if t.name == "stream reader" and t.ident not in before and t.is_alive()] + assert not leaked, "test left a 'stream reader' thread running; close() the interface it created" + + # Use a different base port for the single-node fixture so it doesn't # conflict with the multi-node mesh fixture. SINGLE_NODE_BASE_PORT = DEFAULT_BASE_PORT + 100 diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index a25c0cc9c..fdde5bd95 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -3588,18 +3588,21 @@ def my_sleep(amount): serialInterface = SerialInterface(noProto=True) - with caplog.at_level(logging.DEBUG): - with patch("meshtastic.serial_interface.SerialInterface", return_value=serialInterface): - with patch("time.sleep", side_effect=my_sleep): - with pytest.raises(SystemExit) as pytest_wrapped_e: - tunnelMain() - mock_platform_system.assert_called() - assert pytest_wrapped_e.type == SystemExit - assert pytest_wrapped_e.value.code == 3 - assert re.search(r"Not starting Tunnel", caplog.text, re.MULTILINE) - out, err = capsys.readouterr() - assert re.search(r"Connected to radio", out, re.MULTILINE) - assert err == "" + try: + with caplog.at_level(logging.DEBUG): + with patch("meshtastic.serial_interface.SerialInterface", return_value=serialInterface): + with patch("time.sleep", side_effect=my_sleep): + with pytest.raises(SystemExit) as pytest_wrapped_e: + tunnelMain() + mock_platform_system.assert_called() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 3 + assert re.search(r"Not starting Tunnel", caplog.text, re.MULTILINE) + out, err = capsys.readouterr() + assert re.search(r"Connected to radio", out, re.MULTILINE) + assert err == "" + finally: + serialInterface.close() @pytest.mark.unit diff --git a/meshtastic/tests/test_tcp_interface.py b/meshtastic/tests/test_tcp_interface.py index f9e03d565..8ab53736f 100644 --- a/meshtastic/tests/test_tcp_interface.py +++ b/meshtastic/tests/test_tcp_interface.py @@ -152,10 +152,14 @@ def test_TCPInterface_writeBytes_reconnects(): iface = TCPInterface(hostname="localhost", noProto=True) iface.socket.sendall.side_effect = OSError("Broken pipe") - with patch.object(iface, "_reconnect") as mock_reconnect: - with pytest.raises(OSError, match="Broken pipe"): - iface._writeBytes(b"some data") - mock_reconnect.assert_called_once() + try: + with patch.object(iface, "_reconnect") as mock_reconnect: + with pytest.raises(OSError, match="Broken pipe"): + iface._writeBytes(b"some data") + mock_reconnect.assert_called_once() + finally: + iface.socket.sendall.side_effect = None + iface.close() @pytest.mark.unit diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index e2e917e5b..d6ff3f72b 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -24,9 +24,12 @@ def test_Tunnel_on_non_linux_system(mock_platform_system): a_mock.return_value = "notLinux" mock_platform_system.side_effect = a_mock with patch("socket.socket") as mock_socket: - with pytest.raises(Tunnel.TunnelError) as pytest_wrapped_e: - iface = TCPInterface(hostname="localhost", noProto=True) - Tunnel(iface) + iface = TCPInterface(hostname="localhost", noProto=True) + try: + with pytest.raises(Tunnel.TunnelError) as pytest_wrapped_e: + Tunnel(iface) + finally: + iface.close() assert pytest_wrapped_e.type == Tunnel.TunnelError assert mock_socket.called From c43922072f9f78239e43106dd61cc23e7ce9f2fb Mon Sep 17 00:00:00 2001 From: gchai Date: Tue, 1 Dec 2026 18:04:19 -0500 Subject: [PATCH 2/2] Tighten reader-thread leak guard - Drop dead sendall side-effect reset: noProto close() never writes - Track Thread objects, not idents, which the OS can reuse - Also guard BLEReceive, the BLE reader thread --- meshtastic/tests/conftest.py | 14 +++++++++----- meshtastic/tests/test_tcp_interface.py | 1 - 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/meshtastic/tests/conftest.py b/meshtastic/tests/conftest.py index 7ed92a75a..369b7bc22 100644 --- a/meshtastic/tests/conftest.py +++ b/meshtastic/tests/conftest.py @@ -19,16 +19,20 @@ from .fw_helpers import set_region +# Reader threads that loop on their transport; over a MagicMock transport they spin and record every call. +READER_THREAD_NAMES = frozenset({"stream reader", "BLEReceive"}) + + @pytest.fixture(autouse=True) -def no_leaked_stream_readers(): - """Fail a test that leaves a StreamInterface reader thread running. +def no_leaked_reader_threads(): + """Fail a test that leaves an interface reader thread running. Over a mocked transport the reader spins without sleeping and every MagicMock call is recorded, so a leaked reader grows memory for the rest of the session until the CI runner is killed.""" - before = {t.ident for t in threading.enumerate() if t.name == "stream reader"} + before = set(threading.enumerate()) yield - leaked = [t for t in threading.enumerate() if t.name == "stream reader" and t.ident not in before and t.is_alive()] - assert not leaked, "test left a 'stream reader' thread running; close() the interface it created" + leaked = [t.name for t in threading.enumerate() if t.name in READER_THREAD_NAMES and t not in before] + assert not leaked, f"test left reader thread(s) {leaked} running; close() the interface it created" # Use a different base port for the single-node fixture so it doesn't diff --git a/meshtastic/tests/test_tcp_interface.py b/meshtastic/tests/test_tcp_interface.py index 8ab53736f..5d09d1d07 100644 --- a/meshtastic/tests/test_tcp_interface.py +++ b/meshtastic/tests/test_tcp_interface.py @@ -158,7 +158,6 @@ def test_TCPInterface_writeBytes_reconnects(): iface._writeBytes(b"some data") mock_reconnect.assert_called_once() finally: - iface.socket.sendall.side_effect = None iface.close()