Description
Describe the bug
When trying to connect to a PybricksHub
more than once (and running a program each time), I get the exception:
RuntimeError: disconnected during operation
To reproduce
PC-side:
import asyncio
from pybricksdev.ble import find_device
from pybricksdev.connections.pybricks import PybricksHub
async def main():
dev = await find_device()
print(dev)
hub = PybricksHub()
for _ in range(3):
await hub.connect(dev)
try:
print("starting program...")
await hub.run("brickrail-gui/ble-server/hub_programs/test_noop.py")
print("program stopped!")
finally:
await hub.disconnect()
asyncio.run(main())
Hub program:
import usys
from pybricks.tools import wait
print(usys.version)
print("[hub] program start")
wait(30)
print("[hub] program end")
Output:
1B:BC:1B:A5:81:86: city-green
starting program...
100%|█████████████████████████████████████████████████████████████████████████████████████████████| 213/213 [00:00<00:00, 926B/s]
3.4.0; Pybricks MicroPython v3.2.3 on 2023-02-17
[hub] program start
[hub] program end
program stopped!
starting program...
100%|█████████████████████████████████████████████████████████████████████████████████████████████| 213/213 [00:00<00:00, 887B/s]
Exception has occurred: RuntimeError
disconnected during operation
File "E:\repos\brickrail\brickrail-gui\ble-server\test_connection_pybricksdev.py", line 16, in main
await hub.run("brickrail-gui/ble-server/hub_programs/test_noop.py")
File "E:\repos\brickrail\brickrail-gui\ble-server\test_connection_pybricksdev.py", line 21, in <module>
asyncio.run(main())
RuntimeError: disconnected during operation
The traceback from vscode doesn't track it to the source, here's a traceback from my project that points to the actual point of origin:
Task exception was never retrieved
future: <Task finished name='Task-49' coro=<BLEHub.run.<locals>.run_coroutine() done, defined at E:\repos\brickrail\brickrail-gui\ble-server\ble_hub.py:244> exception=RuntimeError('disconnected during operation')>
Traceback (most recent call last):
File "E:\repos\brickrail\brickrail-gui\ble-server\ble_hub.py", line 247, in run_coroutine
await self.hub.run(program, print_output=False, wait=True)
File "E:\repos\brickrail\brickrail-gui\ble-server\.env\lib\site-packages\pybricksdev\connections\pybricks.py", line 374, in run
await self._wait_for_user_program_stop()
File "E:\repos\brickrail\brickrail-gui\ble-server\.env\lib\site-packages\pybricksdev\connections\pybricks.py", line 454, in _wait_for_user_program_stop
await asyncio.wait_for(
File "E:\repos\brickrail\brickrail-gui\ble-server\.env\lib\asyncio\tasks.py", line 445, in wait_for
return fut.result()
File "E:\repos\brickrail\brickrail-gui\ble-server\.env\lib\site-packages\pybricksdev\connections\pybricks.py", line 286, in race_disconnect
raise RuntimeError("disconnected during operation")
I am using the most recent version of pybricksdev from pip, 1.0.0a37
, and the most recent version of the firmware installed from beta pybricks code, 3.4.0; Pybricks MicroPython v3.2.3 on 2023-02-17
After the exception, the program continues running without problems, and in my project I don't handle the exception, so the hub actually never disconnects ( the light stays on constantly)
When moving the line
hub = PybricksHub()
into the loop, the problem goes away. So I'm guessing the issue must be with some bad state inside PybricksHub
Sometimes, a bit of the output of the program is actually printed the second time the program is run, but I can't reproduce that consistently.
Expected behavior
I would expect multiple connections in serial to work without any issues.