Handle process crashes slightly more gracefully

This commit is contained in:
Guangcong Luo 2018-10-20 17:40:06 -05:00
parent 28625d9efd
commit 4e9ad4e373
2 changed files with 13 additions and 0 deletions

View File

@ -28,10 +28,12 @@ class SubprocessStream extends Streams.ObjectReadWriteStream {
this.process.send(`${taskId}\nNEW`);
}
_write(/** @type {string} */ message) {
if (!process.connected) return;
this.process.send(`${this.taskId}\nWRITE\n${message}`);
// responses are handled in ProcessWrapper
}
_destroy() {
if (!process.connected) return;
this.process.send(`${this.taskId}\nDESTROY`);
}
}
@ -67,6 +69,9 @@ class QueryProcessWrapper {
if (this.resolveRelease && !this.load) this.destroy();
});
this.process.on('disconnect', () => {
this.destroy();
});
}
get load() {

View File

@ -576,6 +576,13 @@ class Battle {
while ((next = await this.stream.read())) {
this.receive(next.split('\n'));
}
if (!this.ended) {
this.room.add(`|bigerror|The simulator process has crashed. We've been notified and will fix this ASAP.`);
Monitor.crashlog(new Error(`Process disconnected`), `A battle`);
this.started = true;
this.ended = true;
this.checkActive();
}
}
receive(/** @type {string[]} */ lines) {
switch (lines[0]) {
@ -960,6 +967,7 @@ class Battle {
}
destroy() {
this.ended = true;
this.stream.destroy();
if (this.active) {
Rooms.global.battleCount += -1;