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 2, 2020
1 parent 0a72196 commit bb8a659
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/core/src/electron-main/electron-main-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,25 @@ export class ElectronMainApplication {
backendProcess.on('error', error => {
reject(error);
});
backendProcess.on('exit', (code, signal) => {
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 issue a kill signal if the backend process is running.
// eslint-disable-next-line no-null/no-null
if (backendProcess.exitCode === null && backendProcess.signalCode === null) {
try {
// 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);
} catch (error) {
// See https://man7.org/linux/man-pages/man2/kill.2.html#ERRORS
if (error.code === 'ESRCH') {
return;
}
throw error;
}
}
});
});
}
Expand Down

0 comments on commit bb8a659

Please sign in to comment.