diff --git a/packaging/aleph-vm/etc/systemd/system/aleph-vm-controller@.service b/packaging/aleph-vm/etc/systemd/system/aleph-vm-controller@.service index c817aad1e..7bbfc67d8 100644 --- a/packaging/aleph-vm/etc/systemd/system/aleph-vm-controller@.service +++ b/packaging/aleph-vm/etc/systemd/system/aleph-vm-controller@.service @@ -11,6 +11,12 @@ WorkingDirectory=/opt/aleph-vm Environment=PYTHONPATH=/opt/aleph-vm/:$PYTHONPATH ExecStart=/usr/bin/python3 -m aleph.vm.controllers --config=/var/lib/aleph/vm/%i-controller.json Restart=on-failure +# KillMode=Mixed is used so initially only the Python controller process receives the SIGTERM signal. +# The controller catches it and sends a QEMU command to shut down the Guest VM, allowing it to clean up +# properly and avoid disk corruption. +# After 30s (TimeoutStopSec), if the process is still running, both the controller and subprocesses receive SIGKILL. +KillMode=mixed +TimeoutStopSec=30 [Install] WantedBy=multi-user.target diff --git a/src/aleph/vm/controllers/__main__.py b/src/aleph/vm/controllers/__main__.py index 19701c4bf..519270b48 100644 --- a/src/aleph/vm/controllers/__main__.py +++ b/src/aleph/vm/controllers/__main__.py @@ -90,6 +90,7 @@ async def handle_persistent_vm(config: Configuration, execution: MicroVM | QemuV def callback(): """Callback for the signal handler to stop the VM and cleanup properly on SIGTERM.""" + logger.debug("Received SIGTERM") loop.create_task(execution.stop()) loop.add_signal_handler(signal.SIGTERM, callback)