Skip to content

Commit

Permalink
electron: fix killing backend
Browse files Browse the repository at this point in the history
Gracefully kill the backend process when exiting an Electron app. A
check is made to make sure we only kill the backend if it is running.
The risk else is that we could kill a random process with the same PID
as the backend had before exiting. This should have been rare but not
impossible.

Signed-off-by: Paul <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Dec 1, 2020
1 parent 0a72196 commit 408df5c
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/core/src/electron-main/electron-main-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,28 @@ export class ElectronMainApplication {
this.processArgv.getProcessArgvWithoutBin(),
await this.getForkOptions(),
);
// Should be true by default, but `.pid` might be undefined if an error occured.
let backendProcessRunning = typeof backendProcess.pid === 'number';
return new Promise((resolve, reject) => {
// The backend server main file is also supposed to send the resolved http(s) server port via IPC.
backendProcess.on('message', (address: AddressInfo) => {
resolve(address.port);
});
backendProcess.on('error', error => {
backendProcessRunning = false;
reject(error);
});
backendProcess.on('exit', (code, signal) => {
backendProcessRunning = false;
console.warn(`backend process (pid: ${backendProcess.pid}) exited with ${typeof code === 'number' ? code : signal}`);
});
app.on('quit', () => {
// If we forked the process for the clusters, we need to manually terminate it.
// See: https://github.com/eclipse-theia/theia/issues/835
process.kill(backendProcess.pid);
// Only kill backend if it is running:
if (backendProcessRunning) {
// If we forked the process for the clusters, we need to manually terminate it.
// See: https://github.com/eclipse-theia/theia/issues/835
process.kill(backendProcess.pid);
}
});
});
}
Expand Down

0 comments on commit 408df5c

Please sign in to comment.