From d4b316962af461cf375e68d228e88b0959a030f7 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Sat, 2 Nov 2024 11:35:47 -0400 Subject: [PATCH] Wait for the state to become stopped in QemuVM#Stop --- src/QemuVM.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/QemuVM.ts b/src/QemuVM.ts index 3a48fbb..eed63e6 100644 --- a/src/QemuVM.ts +++ b/src/QemuVM.ts @@ -160,14 +160,25 @@ export class QemuVM extends EventEmitter { await this.MonitorCommand('system_reset'); } - async Stop() { + async Stop() : Promise { this.AssertState(VMState.Started, 'cannot use QemuVM#Stop on a non-started VM'); + // I'm not sure this is better, but I'm also not sure it should be an assertion + //if(this.state !== VMState.Started) + // return; // Indicate we're stopping, so we don't erroneously start trying to restart everything we're going to tear down. this.SetState(VMState.Stopping); // Stop the QEMU process, which will bring down everything else. await this.StopQemu(); + + // Wait for the VM to reach the stopped state. + return new Promise((res, rej) => { + this.once('statechange', (state) => { + if(state == VMState.Stopped) + res(); + }); + }); } async Reset() {