Fix crash when quickly closing debug log window

This commit is contained in:
Evan Hahn 2021-05-13 18:35:11 -05:00 committed by Scott Nonnenberg
parent 4c249ce5dc
commit e5f9c0db28

View file

@ -104,7 +104,26 @@ export async function initialize(): Promise<pinoms.Logger> {
ipc.on('fetch-log', event => {
fetch(logPath).then(
data => {
try {
event.sender.send('fetched-log', data);
} catch (err: unknown) {
// NOTE(evanhahn): We don't want to send a message to a window that's closed.
// I wanted to use `event.sender.isDestroyed()` but that seems to fail.
// Instead, we attempt the send and catch the failure as best we can.
const hasUserClosedWindow = isProbablyObjectHasBeenDestroyedError(
err
);
if (hasUserClosedWindow) {
logger.info(
'Logs were requested, but it seems the window was closed'
);
} else {
logger.error(
'Problem replying with fetched logs',
err instanceof Error && err.stack ? err.stack : err
);
}
}
},
error => {
logger.error(`Problem loading log from disk: ${error.stack}`);
@ -302,6 +321,10 @@ function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>) {
}
}
function isProbablyObjectHasBeenDestroyedError(err: unknown): boolean {
return err instanceof Error && err.message === 'Object has been destroyed';
}
// This blows up using mocha --watch, so we ensure it is run just once
if (!console._log) {
console._log = console.log;