mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-11 11:56:18 +00:00
G4: F18 — add timeout to serial_cx.connect() event wait
Why: After create_serial_connection, connect() awaited _connected_event.wait() with no timeout. If the serial device opened but connection_made was never called (driver bug, USB adapter glitch), connect() hung indefinitely. Now wrapped in asyncio.wait_for with a configurable timeout (default 10s). asyncio.TimeoutError propagates to the caller for clean failure handling. Refs: Forensics report finding F18
This commit is contained in:
@@ -52,12 +52,16 @@ class SerialConnection:
|
|||||||
def resume_writing(self):
|
def resume_writing(self):
|
||||||
logger.debug("resume writing")
|
logger.debug("resume writing")
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self, timeout: float = 10.0):
|
||||||
"""
|
"""
|
||||||
Connects to the device
|
Connects to the device.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
timeout: Maximum seconds to wait for connection_made callback.
|
||||||
|
Defaults to 10.0. Raises asyncio.TimeoutError on expiry.
|
||||||
"""
|
"""
|
||||||
self._connected_event.clear()
|
self._connected_event.clear()
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
await serial_asyncio.create_serial_connection(
|
await serial_asyncio.create_serial_connection(
|
||||||
loop,
|
loop,
|
||||||
@@ -66,7 +70,7 @@ class SerialConnection:
|
|||||||
baudrate=self.baudrate,
|
baudrate=self.baudrate,
|
||||||
)
|
)
|
||||||
|
|
||||||
await self._connected_event.wait()
|
await asyncio.wait_for(self._connected_event.wait(), timeout=timeout)
|
||||||
logger.info("Serial Connection started")
|
logger.info("Serial Connection started")
|
||||||
return self.port
|
return self.port
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user