From 335071461763e0ae9ef80afcc6b7536e67becd10 Mon Sep 17 00:00:00 2001 From: Vimal Date: Fri, 13 Mar 2026 18:36:03 +0000 Subject: [PATCH] fix(gateway): fix dictionary iteration race condition during stop The stop() method iterates over self.adapters.items() while another async task could be modifying the dict. Copy the dict before iterating to avoid RuntimeError: dictionary changed size during iteration. Fixes race condition that occurred during gateway shutdown. --- gateway/run.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gateway/run.py b/gateway/run.py index 940dcdf01584f..3c02be9e307d8 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -718,7 +718,8 @@ async def stop(self) -> None: logger.info("Stopping gateway...") self._running = False - for platform, adapter in self.adapters.items(): + # Iterate over a copy to avoid "dictionary changed size during iteration" + for platform, adapter in list(self.adapters.items()): try: await adapter.disconnect() logger.info("✓ %s disconnected", platform.value)