Skip to content

Conversation

@derekn
Copy link
Contributor

@derekn derekn commented Apr 14, 2025

The issue identified in #709 occurs when the TCP socket gets disconnected after a period of time. In this case, socket.recv() doesn’t raise an exception but instead returns an empty byte string (b''), which causes the _reader thread to enter a tight infinite loop.

This fix adds a check for that condition and performs a socket reconnect when it occurs.

@CLAassistant
Copy link

CLAassistant commented Apr 14, 2025

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses the 100% CPU usage bug in the TCP interface by adding logic to detect when a disconnected socket returns an empty byte string and then reconnecting accordingly.

  • Added a check for an empty byte string in _readBytes to trigger a socket reconnect
  • Standardized socket checks using "is not None" and updated calls to the superclass
  • Introduced contextlib.suppress to safely handle exceptions during shutdown
Comments suppressed due to low confidence (2)

meshtastic/tcp_interface.py:40

  • [nitpick] Consider renaming 'myConnect' to a more descriptive name such as 'reconnect' or 'establishConnection' to improve code clarity.
self.myConnect()

meshtastic/tcp_interface.py:93

  • [nitpick] Ensure that calling '_startConfig()' after reconnection properly reinitializes all necessary configurations to prevent potential runtime issues.
self._startConfig()

"""
if self.socket: #mian: please check that this should be "if self.socket:"
cast(socket.socket, self.socket).shutdown(socket.SHUT_RDWR)
if self.socket is not None:
Copy link

Copilot AI Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the obsolete inline comment containing 'mian', as it is no longer applicable.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Apr 15, 2025

Codecov Report

Attention: Patch coverage is 64.51613% with 11 lines in your changes missing coverage. Please review.

Project coverage is 59.98%. Comparing base (d4bc391) to head (2e73fe3).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
meshtastic/tcp_interface.py 64.51% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #761      +/-   ##
==========================================
- Coverage   60.13%   59.98%   -0.15%     
==========================================
  Files          24       24              
  Lines        4066     4076      +10     
==========================================
  Hits         2445     2445              
- Misses       1621     1631      +10     
Flag Coverage Δ
unittests 59.98% <64.51%> (-0.15%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ianmcorvidae ianmcorvidae merged commit ee0f73a into meshtastic:master Apr 15, 2025
11 checks passed
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.

3 participants