Fixed the daemon process termination.

From now on, we spawn a detached process that
will periodically check whether the parent Theia
the process is alive, if no, terminates the daemon.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta
2019-07-25 12:01:23 +02:00
parent 83e966d208
commit d29ed24e49
5 changed files with 112 additions and 17 deletions

View File

@@ -1,12 +1,12 @@
import * as which from 'which';
import * as os from 'os';
import { join, delimiter } from 'path';
import { exec } from 'child_process';
import { ChildProcess } from 'child_process';
import { exec, spawn, SpawnOptions } from 'child_process';
import { inject, injectable, named } from 'inversify';
import { ILogger } from '@theia/core/lib/common/logger';
import { BackendApplicationContribution } from '@theia/core/lib/node';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { environment } from '@theia/application-package/lib/environment';
import { DaemonLog } from './daemon-log';
import { ToolOutputServiceServer } from '../common/protocol/tool-output-service';
@@ -23,8 +23,6 @@ export class ArduinoDaemon implements BackendApplicationContribution {
@inject(ToolOutputServiceServer)
protected readonly toolOutputService: ToolOutputServiceServer;
protected process: ChildProcess | undefined;
protected isReady = new Deferred<boolean>();
async onStart() {
@@ -48,6 +46,15 @@ export class ArduinoDaemon implements BackendApplicationContribution {
}
console.log(stdout);
});
const options: SpawnOptions = {
env: environment.electron.runAsNodeEnv(),
detached: true,
stdio: 'ignore'
}
const command = process.execPath;
const cp = spawn(command, [join(__dirname, 'daemon-watcher.js'), String(process.pid), String(daemon.pid)], options);
cp.unref();
if (daemon.stdout) {
daemon.stdout.on('data', data => {
this.toolOutputService.publishNewOutput('daemon', data.toString());
@@ -63,7 +70,6 @@ export class ArduinoDaemon implements BackendApplicationContribution {
if (daemon.stderr) {
daemon.on('exit', (code, signal) => DaemonLog.log(this.logger, `Daemon exited with code: ${code}. Signal was: ${signal}.`));
}
this.process = daemon;
}
await new Promise(resolve => setTimeout(resolve, 2000));
@@ -78,13 +84,4 @@ export class ArduinoDaemon implements BackendApplicationContribution {
}
}
onStop() {
if (!this.process) {
return;
}
DaemonLog.log(this.logger, `Shutting down daemon.`);
this.process.kill("SIGTERM");
}
}

View File

@@ -55,7 +55,7 @@ export class BoardsServiceImpl implements BoardsService {
}
}
}
resolve({boards});
resolve({ boards });
})
});
}

View File

@@ -0,0 +1,17 @@
import * as psTree from 'ps-tree';
const kill = require('tree-kill');
const [theiaPid, daemonPid] = process.argv.slice(2).map(id => Number.parseInt(id, 10));
setInterval(() => {
try {
// Throws an exception if the Theia process doesn't exist anymore.
process.kill(theiaPid, 0);
} catch {
psTree(daemonPid, function (_, children) {
for (const { PID } of children) {
kill(PID);
}
kill(daemonPid, () => process.exit());
});
}
}, 1000);