From e007ca899cfc1db8edde0e959b6d8ad8a9694cf7 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 4 Aug 2021 13:43:07 +0200 Subject: [PATCH] cli - handle unhandled errors (fix #128854) --- src/vs/code/electron-main/app.ts | 18 +++++++++--------- src/vs/code/node/cliProcessMain.ts | 6 +++++- src/vs/platform/files/common/fileService.ts | 5 ++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/vs/code/electron-main/app.ts b/src/vs/code/electron-main/app.ts index 135f444bdbf..10e111a951b 100644 --- a/src/vs/code/electron-main/app.ts +++ b/src/vs/code/electron-main/app.ts @@ -175,8 +175,8 @@ export class CodeApplication extends Disposable { private registerListeners(): void { // We handle uncaught exceptions here to prevent electron from opening a dialog to the user - setUnexpectedErrorHandler(err => this.onUnexpectedError(err)); - process.on('uncaughtException', err => this.onUnexpectedError(err)); + setUnexpectedErrorHandler(error => this.onUnexpectedError(error)); + process.on('uncaughtException', error => onUnexpectedError(error)); process.on('unhandledRejection', (reason: unknown) => onUnexpectedError(reason)); // Dispose on shutdown @@ -328,22 +328,22 @@ export class CodeApplication extends Disposable { return URI.file(path); } - private onUnexpectedError(err: Error): void { - if (err) { + private onUnexpectedError(error: Error): void { + if (error) { // take only the message and stack property const friendlyError = { - message: `[uncaught exception in main]: ${err.message}`, - stack: err.stack + message: `[uncaught exception in main]: ${error.message}`, + stack: error.stack }; // handle on client side this.windowsMainService?.sendToFocused('vscode:reportError', JSON.stringify(friendlyError)); } - this.logService.error(`[uncaught exception in main]: ${err}`); - if (err.stack) { - this.logService.error(err.stack); + this.logService.error(`[uncaught exception in main]: ${error}`); + if (error.stack) { + this.logService.error(error.stack); } } diff --git a/src/vs/code/node/cliProcessMain.ts b/src/vs/code/node/cliProcessMain.ts index c7a53c16d64..0d2fb0dad19 100644 --- a/src/vs/code/node/cliProcessMain.ts +++ b/src/vs/code/node/cliProcessMain.ts @@ -42,7 +42,7 @@ import { ExtensionManagementCLIService } from 'vs/platform/extensionManagement/c import { URI } from 'vs/base/common/uri'; import { LocalizationsService } from 'vs/platform/localizations/node/localizations'; import { ILocalizationsService } from 'vs/platform/localizations/common/localizations'; -import { setUnexpectedErrorHandler } from 'vs/base/common/errors'; +import { onUnexpectedError, setUnexpectedErrorHandler } from 'vs/base/common/errors'; import { toErrorMessage } from 'vs/base/common/errorMessage'; import { VSBuffer } from 'vs/base/common/buffer'; import { cwd } from 'vs/base/common/process'; @@ -189,6 +189,10 @@ class CliMain extends Disposable { logService.error(`[uncaught exception in CLI]: ${message}`); }); + + // Handle unhandled errors that can occur + process.on('uncaughtException', err => onUnexpectedError(err)); + process.on('unhandledRejection', (reason: unknown) => onUnexpectedError(reason)); } private async doRun(environmentService: INativeEnvironmentService, extensionManagementCLIService: IExtensionManagementCLIService, fileService: IFileService): Promise { diff --git a/src/vs/platform/files/common/fileService.ts b/src/vs/platform/files/common/fileService.ts index d6fc1dc63cd..2340d26e85f 100644 --- a/src/vs/platform/files/common/fileService.ts +++ b/src/vs/platform/files/common/fileService.ts @@ -1151,7 +1151,10 @@ export class FileService extends Disposable implements IFileService { override dispose(): void { super.dispose(); - this.activeWatchers.forEach(watcher => dispose(watcher.disposable)); + for (const [, watcher] of this.activeWatchers) { + dispose(watcher.disposable); + } + this.activeWatchers.clear(); }