]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Serialize pid in state and check if still exists on load
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 26 May 2025 15:01:25 +0000 (17:01 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 27 May 2025 08:23:34 +0000 (10:23 +0200)
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

mkosi/qemu.py

index 781adecd4523eff0548f855777118218f0fc2342..c75fa5ba6180f0591a5206487065aca732c63852 100644 (file)
@@ -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,
                 },