Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions bellows/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,21 @@ def connection_lost(self, exc):
"""Port was closed unexpectedly."""

LOGGER.debug("Connection lost: %r", exc)
reason = exc or ConnectionResetError("Remote server closed connection")

# XXX: The startup reset future must be resolved with an error *before* the
# "connection done" future is completed: the secondary thread has an attached
# callback to stop itself, which will cause the a future to propagate a
# `CancelledError` into the active event loop, breaking everything!
if self._startup_reset_future:
self._startup_reset_future.set_exception(
exc or ConnectionResetError("Remote server closed connection")
)
self._startup_reset_future.set_exception(reason)

if self._connection_done_future:
self._connection_done_future.set_result(exc)
self._connection_done_future = None

if self._reset_future:
self._reset_future.cancel()
self._reset_future.set_exception(reason)
self._reset_future = None

if self._send_task:
Expand Down
5 changes: 3 additions & 2 deletions bellows/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ async def connect(self) -> None:

try:
await ezsp.startup_reset()

# Writing config is required here because network info can't be loaded
await ezsp.write_config(self.config[CONF_EZSP_CONFIG])
except Exception:
ezsp.close()
raise

# Writing config is required here because network info can't be loaded otherwise
await ezsp.write_config(self.config[CONF_EZSP_CONFIG])
self._ezsp = ezsp

async def _ensure_network_running(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ async def test_connect_failure(
app: ControllerApplication, ezsp_mock: ezsp.EZSP
) -> None:
"""Test that a failure to connect propagates."""
ezsp_mock.startup_reset = AsyncMock(side_effect=OSError())
ezsp_mock.write_config = AsyncMock(side_effect=OSError())
ezsp_mock.connect = AsyncMock()
app._ezsp = None

Expand Down
2 changes: 1 addition & 1 deletion tests/test_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def on_transport_close():

asyncio.get_running_loop().call_later(0.1, gw.connection_lost, ValueError())

with pytest.raises(asyncio.CancelledError):
with pytest.raises(ValueError):
await gw.reset()

# Need to close to release thread
Expand Down