also wait for reset

(move null assignment to after the process is definitely killed, so that we can actually perform the dispose op)

v0.3.1
This commit is contained in:
Lily Tsuru 2024-11-02 11:43:59 -04:00
parent d4b316962a
commit 2aeba659ed

View file

@ -144,7 +144,6 @@ export class QemuVM extends EventEmitter {
};
}
this.definition.command = cmd;
this.addedAdditionalArguments = true;
}
@ -175,15 +174,27 @@ export class QemuVM extends EventEmitter {
// Wait for the VM to reach the stopped state.
return new Promise((res, rej) => {
this.once('statechange', (state) => {
if(state == VMState.Stopped)
res();
if (state == VMState.Stopped) res();
});
});
}
async Reset() {
async Reset(): Promise<void> {
this.AssertState(VMState.Started, 'cannot use QemuVM#Reset on a non-started VM');
await this.StopQemu();
let self = this;
// Wait for the VM to regain the started state
return new Promise((res, rej) => {
let cb = (state: VMState) => {
if (state == VMState.Started) {
this.removeListener('statechange', cb);
res();
}
};
this.on('statechange', cb);
});
}
async QmpCommand(command: string, args: any | null): Promise<any> {
@ -277,6 +288,7 @@ export class QemuVM extends EventEmitter {
// Dispose events. StartQemu() will assign them again to the new process.
// A bit mucky but /shrug.
self.qemuProcess?.dispose();
self.qemuProcess = null;
self.qmpInstance.reset();
self.qmpInstance.setWriter(null);
@ -296,10 +308,12 @@ export class QemuVM extends EventEmitter {
// Note that we've already tore down everything upon entry to this event handler; therefore
// we can simply set the state and move on.
this.SetState(VMState.Stopped);
self.qemuProcess = null;
}
} else {
// Indicate we have stopped.
this.SetState(VMState.Stopped);
self.qemuProcess = null;
}
});
}
@ -307,7 +321,6 @@ export class QemuVM extends EventEmitter {
private async StopQemu() {
if (this.qemuProcess) {
this.qemuProcess?.kill('SIGTERM');
this.qemuProcess = null;
}
}