From 3818599b94b34174ea73cf94711432de13c94d60 Mon Sep 17 00:00:00 2001 From: Ryan Malloy Date: Thu, 29 Jan 2026 16:23:36 -0700 Subject: [PATCH] Fix port allocation to ignore stopped instances Port allocator was counting stopped QEMU instances as occupying ports, preventing new starts after stop-all. Only running instances now block port reuse. Stopped instances with flash images on disk are preserved for esp_qemu_flash; truly stale entries are purged. --- src/mcp_esptool_server/components/qemu_manager.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/mcp_esptool_server/components/qemu_manager.py b/src/mcp_esptool_server/components/qemu_manager.py index ddc99b3..113d4e0 100644 --- a/src/mcp_esptool_server/components/qemu_manager.py +++ b/src/mcp_esptool_server/components/qemu_manager.py @@ -120,7 +120,7 @@ class QemuManager: def _allocate_port(self) -> int | None: """Find the next available TCP port for a QEMU instance""" - used_ports = {inst.tcp_port for inst in self.instances.values()} + used_ports = {inst.tcp_port for inst in self.instances.values() if inst.is_running} for offset in range(self.config.qemu_max_instances): port = self.config.qemu_base_port + offset if port not in used_ports: @@ -434,6 +434,14 @@ class QemuManager: for inst in list(self.instances.values()): stopped.append(await self._kill_instance(inst)) + # Purge stopped instances that have no flash image on disk (nothing to reuse) + stale = [ + k for k, v in self.instances.items() + if not v.is_running and not v.flash_image.exists() + ] + for k in stale: + del self.instances[k] + return { "success": True, "stopped": [s for s in stopped if s],