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.
This commit is contained in:
Ryan Malloy 2026-01-29 16:23:36 -07:00
parent ff138c492e
commit 3818599b94

View File

@ -120,7 +120,7 @@ class QemuManager:
def _allocate_port(self) -> int | None: def _allocate_port(self) -> int | None:
"""Find the next available TCP port for a QEMU instance""" """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): for offset in range(self.config.qemu_max_instances):
port = self.config.qemu_base_port + offset port = self.config.qemu_base_port + offset
if port not in used_ports: if port not in used_ports:
@ -434,6 +434,14 @@ class QemuManager:
for inst in list(self.instances.values()): for inst in list(self.instances.values()):
stopped.append(await self._kill_instance(inst)) 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 { return {
"success": True, "success": True,
"stopped": [s for s in stopped if s], "stopped": [s for s in stopped if s],