From: Daan De Meyer Date: Mon, 26 May 2025 15:01:25 +0000 (+0200) Subject: Serialize pid in state and check if still exists on load X-Git-Tag: v26~216 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4145382edfb948b362ad56d9cb71ac9913f2b4f4;p=thirdparty%2Fmkosi.git Serialize pid in state and check if still exists on load It happens often enough that mkosi is SIGKILLed and doesn't get to remove the state file, causing it to leak and cause issues. Let's serialize the pid of the mkosi process to the state file and check if it is still running on load. Fixes #3726 --- diff --git a/mkosi/qemu.py b/mkosi/qemu.py index 781adecd4..c75fa5ba6 100644 --- a/mkosi/qemu.py +++ b/mkosi/qemu.py @@ -824,15 +824,19 @@ def finalize_state(config: Config, cid: int) -> Iterator[None]: with flock(statedir): if (p := statedir / f"{config.machine_or_name()}.json").exists(): - die( - f"Another virtual machine named {config.machine_or_name()} is already running", - hint="Use --machine to specify a different virtual machine name", - ) + state = json.loads(p.read_text()) + + if "Pid" not in state or Path(f"/proc/{state['Pid']}").exists(): + die( + f"Another virtual machine named {config.machine_or_name()} is already running", + hint="Use --machine to specify a different virtual machine name", + ) p.write_text( json.dumps( { "Machine": config.machine_or_name(), + "Pid": os.getpid(), "ProxyCommand": f"socat - VSOCK-CONNECT:{cid}:%p", "SshKey": os.fspath(config.ssh_key) if config.ssh_key else None, },