Skip to content

Brokenpipe issue when using TCP connection #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alligitor opened this issue Apr 16, 2025 · 2 comments
Open

Brokenpipe issue when using TCP connection #765

alligitor opened this issue Apr 16, 2025 · 2 comments

Comments

@alligitor
Copy link

I'm using the python API to talk to a Meshtastic instance running on localhost. this is a Raspberry Pi 3B, if that matters.

Every so often, it gets the following error:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 1394, in run
    self.function(*self.args, **self.kwargs)
  File "/home/pi/meshtastic/lib/python3.11/site-packages/meshtastic/mesh_interface.py", line 1116, in callback
    self.sendHeartbeat()
  File "/home/pi/meshtastic/lib/python3.11/site-packages/meshtastic/mesh_interface.py", line 1105, in sendHeartbeat
    self._sendToRadio(p)
  File "/home/pi/meshtastic/lib/python3.11/site-packages/meshtastic/mesh_interface.py", line 1180, in _sendToRadio
    self._sendToRadioImpl(toRadio)
  File "/home/pi/meshtastic/lib/python3.11/site-packages/meshtastic/stream_interface.py", line 120, in _sendToRadioImpl
    self._writeBytes(header + b)
  File "/home/pi/meshtastic/lib/python3.11/site-packages/meshtastic/tcp_interface.py", line 79, in _writeBytes
    self.socket.send(b)
BrokenPipeError: [Errno 32] Broken pipe

I saw the following article and tried the meshtastic.connection.lost, but that does not get called when this error happens.

#727

using try/except doesn't help because the error happens in a thread that the Meshtastic python lib kicks off.

the following property, doesn't change either when this error happens:
interface.isConnected

Looking at the python code, I don't see any error handling for when _writeBytes fails.

Any thoughts on how best to handle this error?

@roberthadow
Copy link

Brokenpipe is a common issue. In a more general sense, the meshtastic code needs to built with the same design criteria as all embedded code--a graceful exit from every possible error.

When we stick a solar-powered device high in a tree and turn off WiFi to conserve power, we shouldn't have to climb the tree to retrive the device, open the enclosure and touch the reset button. the device needs to recover, reset, and resume.

@alligitor
Copy link
Author

I ended up getting around this by doing the following. The code reaches in the interface object and uses socket calls to get the status directly from the network socket. Not the best, but works.


def isSocketConnected(sock):
    r, _, _ = select.select([sock], [], [], 0)
    if r:
        data = sock.recv(1, socket.MSG_PEEK)
        if not data:
            return False
    return True



try:
    #this line connects to meshtasticd
    interface = meshtastic.tcp_interface.TCPInterface(hostname='localhost')

    while True:
        time.sleep(2)

        #keep an eye on the socket to see if it closed
        if isSocketConnected(interface.socket):
            None
            #print("Socket is connected")
        else:
            print("Socket is disconnected")
            interface.close()
            exit(4)

except :
    print("meshtastic.tcp_interface.TCPInterface failed")

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

No branches or pull requests

2 participants