diff --git a/src/aleph/vm/controllers/__main__.py b/src/aleph/vm/controllers/__main__.py index 32640bf6e..3232c82bf 100644 --- a/src/aleph/vm/controllers/__main__.py +++ b/src/aleph/vm/controllers/__main__.py @@ -82,7 +82,12 @@ async def execute_persistent_vm(config: Configuration): async def handle_persistent_vm(config: Configuration, execution: Union[MicroVM, QemuVM], process: Process): # Catch the terminating signal and send a proper message to the vm to stop it so it close files properly loop = asyncio.get_event_loop() - loop.add_signal_handler(signal.SIGTERM, execution.send_shutdown_message) + + def callback(): + """Callback for the signal handler to stop the VM and cleanup properly on SIGTERM.""" + loop.create_task(execution.teardown()) + + loop.add_signal_handler(signal.SIGTERM, callback) await process.wait() logger.info(f"Process terminated with {process.returncode}") diff --git a/src/aleph/vm/hypervisors/qemu/qemuvm.py b/src/aleph/vm/hypervisors/qemu/qemuvm.py index b4e577dde..0d49403c8 100644 --- a/src/aleph/vm/hypervisors/qemu/qemuvm.py +++ b/src/aleph/vm/hypervisors/qemu/qemuvm.py @@ -125,3 +125,7 @@ def send_shutdown_message(self): logger.warning("unexpected answer from VM", resp) print("shutdown message sent") client.close() + + async def teardown(self): + """Stop the VM.""" + self.send_shutdown_message()