martinohanlon / BlueDot

A zero boiler plate bluetooth remote
MIT License
141 stars 43 forks source link

Randomly getting ConnectionAbortedError on receive data and 'Transport endpoint is not connected' on send data errors #185

Open wallem89 opened 1 year ago

wallem89 commented 1 year ago

Describe the bug I am running this software on two Raspberry Pi's and one is the server an the other Pi is the client. Both are used to send and receive data. I am getting both issues randomly at the client side. Both programs are quite identical since both are waiting for new data to come in with the data_received_callback and both are sending data with the sendfunction. But the server with the send function from BluetoothServer and the client from BluetoothClient.

ConnectionAbortedError in receive thread:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 716, in _read
    self._handle_bt_error(e)
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 746, in _handle_bt_error
    raise bt_error
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 714, in _read
    data = self._client_sock.recv(1024, socket.MSG_DONTWAIT)
ConnectionAbortedError: [Errno 103] Software caused connection abort

Transport endpoint is not connected error during sending:

ERROR:grpc._server:Exception calling application: [Errno 107] Transport endpoint is not connected
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/grpc/_server.py", line 494, in _call_behavior
    response_or_iterator = behavior(argument, context)
  File "./client.py", line 127, in send
    self.client.send(data)
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 698, in send
    self._handle_bt_error(e)
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 746, in _handle_bt_error
    raise bt_error
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 696, in send
    self._send_data(data)
  File "/usr/local/lib/python3.7/site-packages/bluedot/btcomm.py", line 707, in _send_data
    self._client_sock.sendall(data)
OSError: [Errno 107] Transport endpoint is not connected

To Reproduce Steps to reproduce the behavior:

  1. Set-up a BluetoothAdapter on both client and server side
  2. Start on server side with BluetoothServer and the client with the BluetoothClient
  3. Have a data_received_callback function on both sides waiting for new data to come
  4. On both sides send data with the respective send functions.
  5. Wait.. (time various)

Expected behavior A reliable connection that even when the bluetooth connection disconnects or the other side is outside range the program doesn't crash. I want a clear error message and be able to connect the client to the server after they have disconnected.

System (please complete the following information):

Additional context Both programs using the build-in Bluetooth adapter of the Raspberry Pi 4.

martinohanlon commented 1 year ago

You dont say how long you have to wait before you experience a disconnect. Although I am not completely surprised, no connection (certainly not BT) is going to reliable for long periods of time and dealing with disconnects should be expected.

I dont think its the responsibility of the btcomm API to maintain long term connections, however I dont think it would be too difficult to amend the calling program to deal with disconnects and reconnect as appropriate.

I would probably do 2 things: