Skip to content

Close stream interfaces leaked by unit tests (fixes 3.14/3.15 CI runner kills) - #1028

Open
gchai wants to merge 2 commits into
meshtastic:masterfrom
gchai:fix/tests-leaked-reader-threads
Open

gchai wants to merge 2 commits into
meshtastic:masterfrom
gchai:fix/tests-leaked-reader-threads

Conversation

@gchai

@gchai gchai commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes the intermittent build (3.14) / build (3.15.2) failures on master and on PRs: the runner either "lost communication with the server" or got a shutdown signal and make: *** [Makefile:55: ci-test] Killed, often after every test had already passed.

Cause

Three unit tests start a StreamInterface reader thread over a mocked transport and never close the interface:

  • test_main.py::test_tunnel_tunnel_arg (SerialInterface, mocked serial.Serial)
  • test_tcp_interface.py::test_TCPInterface_writeBytes_reconnects (TCPInterface, mocked socket.socket)
  • test_tunnel.py::test_Tunnel_on_non_linux_system (same)

The reader loop never sleeps here: read(1) / recv(1) returns a MagicMock whose len() is 0, so it immediately reads again. MagicMock records every call, so memory grows for the rest of the session, and the threads compete for the GIL. At the end, pytest's unraisable-exception cleanup runs gc.collect() five times over that heap while the threads keep allocating. On 3.14+ this outlasts the runner's memory. Older versions usually finish first.

Locally on Python 3.14, pytest --cov=meshtastic --cov-report=xml on master passed every test in 259 s, then hung in gc_collect_harder with 1-3 GB RSS; a thread dump showed the three stream reader threads in mock._increment_mock_call. With those three tests deselected: 18 s, exit 0, peak RSS 312 MB.

Fix

  • Close the interface in each of the three tests (try/finally).
  • Add an autouse fixture in conftest.py that fails any test leaving a new stream reader or BLEReceive thread alive, so the next leak shows up as a named test failure instead of a dead runner.

Testing

  • pytest --cov=meshtastic --cov-report=xml on Python 3.14: 570 passed, 2 skipped in 18.9 s, and the process exits (this env lacks a few optional extras, hence fewer tests than CI).
  • With test_tunnel.py reverted, the new fixture fails test_Tunnel_on_non_linux_system with the leak message.
  • pylint 10.00/10 on the changed files.

Summary by CodeRabbit

  • Tests
    • Improved test cleanup by ensuring network and tunnel interfaces are closed even when checks fail.
    • Added checks to detect reader threads left running after tests, helping catch resource leaks.
    • Expanded coverage around TCP reconnection and tunnel behavior.

gchai added 2 commits December 1, 2026 18:02
- 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
- 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
@gchai
gchai marked this pull request as ready for review December 1, 2026 23:05
@coderabbitai

coderabbitai Bot commented Dec 1, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Tests now close interfaces in three test cases, including when assertions fail. An autouse fixture checks whether new threads named stream reader or BLEReceive remain running after each test.

Changes: Test resource cleanup

Layer / File(s) Summary
Interface cleanup and thread detection
meshtastic/tests/conftest.py, meshtastic/tests/test_main.py, meshtastic/tests/test_tcp_interface.py, meshtastic/tests/test_tunnel.py
An autouse fixture checks for newly started stream reader and BLEReceive threads that remain running. Three tests close their interfaces in finally blocks. The tunnel test captures output after the patched interface context exits.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Suggested reviewers: mkinney, ianmcorvidae, caveman99

Merge Risk: Minimal · up to c4392

This change only closes interfaces in tests and adds a leak guard, so it does not affect runtime behavior. It looks ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: closing stream interfaces that unit tests leaked, which the objectives describe as the fix for the 3.14/3.15 CI runner kills. The changeset closes the SerialInterface …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: closing stream interfaces that unit tests leaked, which the objectives describe as the fix for the 3.14/3.15 CI runner kills. The changeset closes the SerialInterface and TCPInterface instances in try/finally blocks and adds a conftest.py fixture that fails tests leaving stream reader or BLEReceive threads alive. The title also states the CI symptom it addresses. It is 78 characters, slightly above a short title, but it stays specific and on topic.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Pylint (4.1.3)

Pylint used default settings because repository settings are unavailable in automated reviews.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 795a0f0 and c439220.

Files selected for processing: 4

  • meshtastic/tests/conftest.py
  • meshtastic/tests/test_main.py
  • meshtastic/tests/test_tcp_interface.py
  • meshtastic/tests/test_tunnel.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant