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

@ -89,7 +89,7 @@ export class QemuVM extends EventEmitter {
// Fall back to the default process launcher. This is // Fall back to the default process launcher. This is
// done so we can have our cake (compatibility) and eat it too // done so we can have our cake (compatibility) and eat it too
// (do this fun process abstraction stuff for whatever really) // (do this fun process abstraction stuff for whatever really)
if(!processLauncher) { if (!processLauncher) {
this.qemuLauncher = new DefaultProcessLauncher(); this.qemuLauncher = new DefaultProcessLauncher();
} else { } else {
this.qemuLauncher = processLauncher; this.qemuLauncher = processLauncher;
@ -144,7 +144,6 @@ export class QemuVM extends EventEmitter {
}; };
} }
this.definition.command = cmd; this.definition.command = cmd;
this.addedAdditionalArguments = true; this.addedAdditionalArguments = true;
} }
@ -160,7 +159,7 @@ export class QemuVM extends EventEmitter {
await this.MonitorCommand('system_reset'); await this.MonitorCommand('system_reset');
} }
async Stop() : Promise<void> { async Stop(): Promise<void> {
this.AssertState(VMState.Started, 'cannot use QemuVM#Stop on a non-started VM'); 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 // I'm not sure this is better, but I'm also not sure it should be an assertion
//if(this.state !== VMState.Started) //if(this.state !== VMState.Started)
@ -175,15 +174,27 @@ export class QemuVM extends EventEmitter {
// Wait for the VM to reach the stopped state. // Wait for the VM to reach the stopped state.
return new Promise((res, rej) => { return new Promise((res, rej) => {
this.once('statechange', (state) => { this.once('statechange', (state) => {
if(state == VMState.Stopped) if (state == VMState.Stopped) res();
res();
}); });
}); });
} }
async Reset() { async Reset(): Promise<void> {
this.AssertState(VMState.Started, 'cannot use QemuVM#Reset on a non-started VM'); this.AssertState(VMState.Started, 'cannot use QemuVM#Reset on a non-started VM');
await this.StopQemu(); 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> { 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. // Dispose events. StartQemu() will assign them again to the new process.
// A bit mucky but /shrug. // A bit mucky but /shrug.
self.qemuProcess?.dispose(); self.qemuProcess?.dispose();
self.qemuProcess = null;
self.qmpInstance.reset(); self.qmpInstance.reset();
self.qmpInstance.setWriter(null); 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 // Note that we've already tore down everything upon entry to this event handler; therefore
// we can simply set the state and move on. // we can simply set the state and move on.
this.SetState(VMState.Stopped); this.SetState(VMState.Stopped);
self.qemuProcess = null;
} }
} else { } else {
// Indicate we have stopped. // Indicate we have stopped.
this.SetState(VMState.Stopped); this.SetState(VMState.Stopped);
self.qemuProcess = null;
} }
}); });
} }
@ -307,7 +321,6 @@ export class QemuVM extends EventEmitter {
private async StopQemu() { private async StopQemu() {
if (this.qemuProcess) { if (this.qemuProcess) {
this.qemuProcess?.kill('SIGTERM'); this.qemuProcess?.kill('SIGTERM');
this.qemuProcess = null;
} }
} }