Path: blob/main/src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts
5248 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import electron from 'electron';6import { validatedIpcMain } from '../../../base/parts/ipc/electron-main/ipcMain.js';7import { Barrier, Promises, timeout } from '../../../base/common/async.js';8import { Emitter, Event } from '../../../base/common/event.js';9import { Disposable, DisposableStore } from '../../../base/common/lifecycle.js';10import { isMacintosh, isWindows } from '../../../base/common/platform.js';11import { cwd } from '../../../base/common/process.js';12import { assertReturnsDefined } from '../../../base/common/types.js';13import { NativeParsedArgs } from '../../environment/common/argv.js';14import { createDecorator } from '../../instantiation/common/instantiation.js';15import { ILogService } from '../../log/common/log.js';16import { IStateService } from '../../state/node/state.js';17import { ICodeWindow, LoadReason, UnloadReason } from '../../window/electron-main/window.js';18import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from '../../workspace/common/workspace.js';19import { IEnvironmentMainService } from '../../environment/electron-main/environmentMainService.js';20import { IAuxiliaryWindow } from '../../auxiliaryWindow/electron-main/auxiliaryWindow.js';21import { getAllWindowsExcludingOffscreen } from '../../windows/electron-main/windows.js';2223export const ILifecycleMainService = createDecorator<ILifecycleMainService>('lifecycleMainService');2425interface WindowLoadEvent {2627/**28* The window that is loaded to a new workspace.29*/30readonly window: ICodeWindow;3132/**33* The workspace the window is loaded into.34*/35readonly workspace: IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | undefined;3637/**38* More details why the window loads to a new workspace.39*/40readonly reason: LoadReason;41}4243export const enum ShutdownReason {4445/**46* The application exits normally.47*/48QUIT = 1,4950/**51* The application exits abnormally and is being52* killed with an exit code (e.g. from integration53* test run)54*/55KILL56}5758export interface ShutdownEvent {5960/**61* More details why the application is shutting down.62*/63reason: ShutdownReason;6465/**66* Allows to join the shutdown. The promise can be a long running operation but it67* will block the application from closing.68*/69join(id: string, promise: Promise<void>): void;70}7172export interface IRelaunchHandler {7374/**75* Allows a handler to deal with relaunching the application. The return76* value indicates if the relaunch is handled or not.77*/78handleRelaunch(options?: IRelaunchOptions): boolean;79}8081export interface IRelaunchOptions {82readonly addArgs?: string[];83readonly removeArgs?: string[];84}8586export interface ILifecycleMainService {8788readonly _serviceBrand: undefined;8990/**91* Will be true if the program was restarted (e.g. due to explicit request or update).92*/93readonly wasRestarted: boolean;9495/**96* Will be true if the program was requested to quit.97*/98readonly quitRequested: boolean;99100/**101* A flag indicating in what phase of the lifecycle we currently are.102*/103phase: LifecycleMainPhase;104105/**106* An event that fires when the application is about to shutdown before any window is closed.107* The shutdown can still be prevented by any window that vetos this event.108*/109readonly onBeforeShutdown: Event<void>;110111/**112* An event that fires after the onBeforeShutdown event has been fired and after no window has113* vetoed the shutdown sequence. At this point listeners are ensured that the application will114* quit without veto.115*/116readonly onWillShutdown: Event<ShutdownEvent>;117118/**119* An event that fires when a window is loading. This can either be a window opening for the120* first time or a window reloading or changing to another URL.121*/122readonly onWillLoadWindow: Event<WindowLoadEvent>;123124/**125* An event that fires before a window closes. This event is fired after any veto has been dealt126* with so that listeners know for sure that the window will close without veto.127*/128readonly onBeforeCloseWindow: Event<ICodeWindow>;129130/**131* Make a `ICodeWindow` known to the lifecycle main service.132*/133registerWindow(window: ICodeWindow): void;134135/**136* Make a `IAuxiliaryWindow` known to the lifecycle main service.137*/138registerAuxWindow(auxWindow: IAuxiliaryWindow): void;139140/**141* Reload a window. All lifecycle event handlers are triggered.142*/143reload(window: ICodeWindow, cli?: NativeParsedArgs): Promise<void>;144145/**146* Unload a window for the provided reason. All lifecycle event handlers are triggered.147*/148unload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */>;149150/**151* Restart the application with optional arguments (CLI). All lifecycle event handlers are triggered.152*/153relaunch(options?: IRelaunchOptions): Promise<void>;154155/**156* Sets a custom handler for relaunching the application.157*/158setRelaunchHandler(handler: IRelaunchHandler): void;159160/**161* Shutdown the application normally. All lifecycle event handlers are triggered.162*/163quit(willRestart?: boolean): Promise<boolean /* veto */>;164165/**166* Forcefully shutdown the application and optionally set an exit code.167*168* This method should only be used in rare situations where it is important169* to set an exit code (e.g. running tests) or when the application is170* not in a healthy state and should terminate asap.171*172* This method does not fire the normal lifecycle events to the windows,173* that normally can be vetoed. Windows are destroyed without a chance174* of components to participate. The only lifecycle event handler that175* is triggered is `onWillShutdown` in the main process.176*/177kill(code?: number): Promise<void>;178179/**180* Returns a promise that resolves when a certain lifecycle phase181* has started.182*/183when(phase: LifecycleMainPhase): Promise<void>;184}185186export const enum LifecycleMainPhase {187188/**189* The first phase signals that we are about to startup.190*/191Starting = 1,192193/**194* Services are ready and first window is about to open.195*/196Ready = 2,197198/**199* This phase signals a point in time after the window has opened200* and is typically the best place to do work that is not required201* for the window to open.202*/203AfterWindowOpen = 3,204205/**206* The last phase after a window has opened and some time has passed207* (2-5 seconds).208*/209Eventually = 4210}211212export class LifecycleMainService extends Disposable implements ILifecycleMainService {213214declare readonly _serviceBrand: undefined;215216private static readonly QUIT_AND_RESTART_KEY = 'lifecycle.quitAndRestart';217218private readonly _onBeforeShutdown = this._register(new Emitter<void>());219readonly onBeforeShutdown = this._onBeforeShutdown.event;220221private readonly _onWillShutdown = this._register(new Emitter<ShutdownEvent>());222readonly onWillShutdown = this._onWillShutdown.event;223224private readonly _onWillLoadWindow = this._register(new Emitter<WindowLoadEvent>());225readonly onWillLoadWindow = this._onWillLoadWindow.event;226227private readonly _onBeforeCloseWindow = this._register(new Emitter<ICodeWindow>());228readonly onBeforeCloseWindow = this._onBeforeCloseWindow.event;229230private _quitRequested = false;231get quitRequested(): boolean { return this._quitRequested; }232233private _wasRestarted = false;234get wasRestarted(): boolean { return this._wasRestarted; }235236private _phase = LifecycleMainPhase.Starting;237get phase(): LifecycleMainPhase { return this._phase; }238239private readonly windowToCloseRequest = new Set<number>();240private oneTimeListenerTokenGenerator = 0;241private windowCounter = 0;242243private pendingQuitPromise: Promise<boolean> | undefined = undefined;244private pendingQuitPromiseResolve: { (veto: boolean): void } | undefined = undefined;245246private pendingWillShutdownPromise: Promise<void> | undefined = undefined;247248private readonly mapWindowIdToPendingUnload = new Map<number, Promise<boolean>>();249250private readonly phaseWhen = new Map<LifecycleMainPhase, Barrier>();251252private relaunchHandler: IRelaunchHandler | undefined = undefined;253254constructor(255@ILogService private readonly logService: ILogService,256@IStateService private readonly stateService: IStateService,257@IEnvironmentMainService private readonly environmentMainService: IEnvironmentMainService258) {259super();260261this.resolveRestarted();262this.when(LifecycleMainPhase.Ready).then(() => this.registerListeners());263}264265private resolveRestarted(): void {266this._wasRestarted = !!this.stateService.getItem(LifecycleMainService.QUIT_AND_RESTART_KEY);267268if (this._wasRestarted) {269// remove the marker right after if found270this.stateService.removeItem(LifecycleMainService.QUIT_AND_RESTART_KEY);271}272}273274private registerListeners(): void {275276// before-quit: an event that is fired if application quit was277// requested but before any window was closed.278const beforeQuitListener = () => {279if (this._quitRequested) {280return;281}282283this.trace('Lifecycle#app.on(before-quit)');284this._quitRequested = true;285286// Emit event to indicate that we are about to shutdown287this.trace('Lifecycle#onBeforeShutdown.fire()');288this._onBeforeShutdown.fire();289290// macOS: can run without any window open. in that case we fire291// the onWillShutdown() event directly because there is no veto292// to be expected.293if (isMacintosh && this.windowCounter === 0) {294this.fireOnWillShutdown(ShutdownReason.QUIT);295}296};297electron.app.addListener('before-quit', beforeQuitListener);298299// window-all-closed: an event that only fires when the last window300// was closed. We override this event to be in charge if app.quit()301// should be called or not.302const windowAllClosedListener = () => {303this.trace('Lifecycle#app.on(window-all-closed)');304305// Windows/Linux: we quit when all windows have closed306// Mac: we only quit when quit was requested307if (this._quitRequested || !isMacintosh) {308electron.app.quit();309}310};311electron.app.addListener('window-all-closed', windowAllClosedListener);312313// will-quit: an event that is fired after all windows have been314// closed, but before actually quitting.315electron.app.once('will-quit', e => {316this.trace('Lifecycle#app.on(will-quit) - begin');317318// Prevent the quit until the shutdown promise was resolved319e.preventDefault();320321// Start shutdown sequence322const shutdownPromise = this.fireOnWillShutdown(ShutdownReason.QUIT);323324// Wait until shutdown is signaled to be complete325shutdownPromise.finally(() => {326this.trace('Lifecycle#app.on(will-quit) - after fireOnWillShutdown');327328// Resolve pending quit promise now without veto329this.resolvePendingQuitPromise(false /* no veto */);330331// Quit again, this time do not prevent this, since our332// will-quit listener is only installed "once". Also333// remove any listener we have that is no longer needed334335electron.app.removeListener('before-quit', beforeQuitListener);336electron.app.removeListener('window-all-closed', windowAllClosedListener);337338this.trace('Lifecycle#app.on(will-quit) - calling app.quit()');339340electron.app.quit();341});342});343}344345private fireOnWillShutdown(reason: ShutdownReason): Promise<void> {346if (this.pendingWillShutdownPromise) {347return this.pendingWillShutdownPromise; // shutdown is already running348}349350const logService = this.logService;351this.trace('Lifecycle#onWillShutdown.fire()');352353const joiners: Promise<void>[] = [];354355this._onWillShutdown.fire({356reason,357join(id, promise) {358logService.trace(`Lifecycle#onWillShutdown - begin '${id}'`);359joiners.push(promise.finally(() => {360logService.trace(`Lifecycle#onWillShutdown - end '${id}'`);361}));362}363});364365this.pendingWillShutdownPromise = (async () => {366367// Settle all shutdown event joiners368try {369await Promises.settled(joiners);370} catch (error) {371this.logService.error(error);372}373374// Then, always make sure at the end375// the state service is flushed.376try {377await this.stateService.close();378} catch (error) {379this.logService.error(error);380}381})();382383return this.pendingWillShutdownPromise;384}385386set phase(value: LifecycleMainPhase) {387if (value < this.phase) {388throw new Error('Lifecycle cannot go backwards');389}390391if (this._phase === value) {392return;393}394395this.trace(`lifecycle (main): phase changed (value: ${value})`);396397this._phase = value;398399const barrier = this.phaseWhen.get(this._phase);400if (barrier) {401barrier.open();402this.phaseWhen.delete(this._phase);403}404}405406async when(phase: LifecycleMainPhase): Promise<void> {407if (phase <= this._phase) {408return;409}410411let barrier = this.phaseWhen.get(phase);412if (!barrier) {413barrier = new Barrier();414this.phaseWhen.set(phase, barrier);415}416417await barrier.wait();418}419420registerWindow(window: ICodeWindow): void {421const windowListeners = new DisposableStore();422423// track window count424this.windowCounter++;425426// Window Will Load427windowListeners.add(window.onWillLoad(e => this._onWillLoadWindow.fire({ window, workspace: e.workspace, reason: e.reason })));428429// Window Before Closing: Main -> Renderer430const win = assertReturnsDefined(window.win);431windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'close')(e => {432433// The window already acknowledged to be closed434const windowId = window.id;435if (this.windowToCloseRequest.delete(windowId)) {436return;437}438439this.trace(`Lifecycle#window.on('close') - window ID ${window.id}`);440441// Otherwise prevent unload and handle it from window442e.preventDefault();443this.unload(window, UnloadReason.CLOSE).then(veto => {444if (veto) {445this.windowToCloseRequest.delete(windowId);446return;447}448449this.windowToCloseRequest.add(windowId);450451// Fire onBeforeCloseWindow before actually closing452this.trace(`Lifecycle#onBeforeCloseWindow.fire() - window ID ${windowId}`);453this._onBeforeCloseWindow.fire(window);454455// No veto, close window now456window.close();457});458}));459windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'closed')(() => {460this.trace(`Lifecycle#window.on('closed') - window ID ${window.id}`);461462// update window count463this.windowCounter--;464465// clear window listeners466windowListeners.dispose();467468// if there are no more code windows opened, fire the onWillShutdown event, unless469// we are on macOS where it is perfectly fine to close the last window and470// the application continues running (unless quit was actually requested)471if (this.windowCounter === 0 && (!isMacintosh || this._quitRequested)) {472this.fireOnWillShutdown(ShutdownReason.QUIT);473}474}));475}476477registerAuxWindow(auxWindow: IAuxiliaryWindow): void {478const win = assertReturnsDefined(auxWindow.win);479480const windowListeners = new DisposableStore();481windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'close')(e => {482this.trace(`Lifecycle#auxWindow.on('close') - window ID ${auxWindow.id}`);483484if (this._quitRequested) {485this.trace(`Lifecycle#auxWindow.on('close') - preventDefault() because quit requested`);486487// When quit is requested, Electron will close all488// auxiliary windows before closing the main windows.489// This prevents us from storing the auxiliary window490// state on shutdown and thus we prevent closing if491// quit is requested.492//493// Interestingly, this will not prevent the application494// from quitting because the auxiliary windows will still495// close once the owning window closes.496497e.preventDefault();498}499}));500windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'closed')(() => {501this.trace(`Lifecycle#auxWindow.on('closed') - window ID ${auxWindow.id}`);502503windowListeners.dispose();504}));505}506507async reload(window: ICodeWindow, cli?: NativeParsedArgs): Promise<void> {508509// Only reload when the window has not vetoed this510const veto = await this.unload(window, UnloadReason.RELOAD);511if (!veto) {512window.reload(cli);513}514}515516unload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {517518// Ensure there is only 1 unload running at the same time519const pendingUnloadPromise = this.mapWindowIdToPendingUnload.get(window.id);520if (pendingUnloadPromise) {521return pendingUnloadPromise;522}523524// Start unload and remember in map until finished525const unloadPromise = this.doUnload(window, reason).finally(() => {526this.mapWindowIdToPendingUnload.delete(window.id);527});528this.mapWindowIdToPendingUnload.set(window.id, unloadPromise);529530return unloadPromise;531}532533private async doUnload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {534535// Always allow to unload a window that is not yet ready536if (!window.isReady) {537return false;538}539540this.trace(`Lifecycle#unload() - window ID ${window.id}`);541542// first ask the window itself if it vetos the unload543const windowUnloadReason = this._quitRequested ? UnloadReason.QUIT : reason;544const veto = await this.onBeforeUnloadWindowInRenderer(window, windowUnloadReason);545if (veto) {546this.trace(`Lifecycle#unload() - veto in renderer (window ID ${window.id})`);547548return this.handleWindowUnloadVeto(veto);549}550551// finally if there are no vetos, unload the renderer552await this.onWillUnloadWindowInRenderer(window, windowUnloadReason);553554return false;555}556557private handleWindowUnloadVeto(veto: boolean): boolean {558if (!veto) {559return false; // no veto560}561562// a veto resolves any pending quit with veto563this.resolvePendingQuitPromise(true /* veto */);564565// a veto resets the pending quit request flag566this._quitRequested = false;567568return true; // veto569}570571private resolvePendingQuitPromise(veto: boolean): void {572if (this.pendingQuitPromiseResolve) {573this.pendingQuitPromiseResolve(veto);574this.pendingQuitPromiseResolve = undefined;575this.pendingQuitPromise = undefined;576}577}578579private onBeforeUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {580return new Promise<boolean>(resolve => {581const oneTimeEventToken = this.oneTimeListenerTokenGenerator++;582const okChannel = `vscode:ok${oneTimeEventToken}`;583const cancelChannel = `vscode:cancel${oneTimeEventToken}`;584585validatedIpcMain.once(okChannel, () => {586resolve(false); // no veto587});588589validatedIpcMain.once(cancelChannel, () => {590resolve(true); // veto591});592593window.send('vscode:onBeforeUnload', { okChannel, cancelChannel, reason });594});595}596597private onWillUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): Promise<void> {598return new Promise<void>(resolve => {599const oneTimeEventToken = this.oneTimeListenerTokenGenerator++;600const replyChannel = `vscode:reply${oneTimeEventToken}`;601602validatedIpcMain.once(replyChannel, () => resolve());603604window.send('vscode:onWillUnload', { replyChannel, reason });605});606}607608quit(willRestart?: boolean): Promise<boolean /* veto */> {609return this.doQuit(willRestart).then(veto => {610if (!veto && willRestart) {611// Windows: we are about to restart and as such we need to restore the original612// current working directory we had on startup to get the exact same startup613// behaviour. As such, we briefly change back to that directory and then when614// Code starts it will set it back to the installation directory again.615try {616if (isWindows) {617const currentWorkingDir = cwd();618if (currentWorkingDir !== process.cwd()) {619process.chdir(currentWorkingDir);620}621}622} catch (err) {623this.logService.error(err);624}625}626627return veto;628});629}630631private doQuit(willRestart?: boolean): Promise<boolean /* veto */> {632this.trace(`Lifecycle#quit() - begin (willRestart: ${willRestart})`);633634if (this.pendingQuitPromise) {635this.trace('Lifecycle#quit() - returning pending quit promise');636637return this.pendingQuitPromise;638}639640// Remember if we are about to restart641if (willRestart) {642this.stateService.setItem(LifecycleMainService.QUIT_AND_RESTART_KEY, true);643}644645this.pendingQuitPromise = new Promise(resolve => {646647// Store as field to access it from a window cancellation648this.pendingQuitPromiseResolve = resolve;649650// Calling app.quit() will trigger the close handlers of each opened window651// and only if no window vetoed the shutdown, we will get the will-quit event652this.trace('Lifecycle#quit() - calling app.quit()');653electron.app.quit();654});655656return this.pendingQuitPromise;657}658659private trace(msg: string): void {660if (this.environmentMainService.args['enable-smoke-test-driver']) {661this.logService.info(msg); // helps diagnose issues with exiting from smoke tests662} else {663this.logService.trace(msg);664}665}666667setRelaunchHandler(handler: IRelaunchHandler): void {668this.relaunchHandler = handler;669}670671async relaunch(options?: IRelaunchOptions): Promise<void> {672this.trace('Lifecycle#relaunch()');673674const args = process.argv.slice(1);675if (options?.addArgs) {676args.push(...options.addArgs);677}678679if (options?.removeArgs) {680for (const a of options.removeArgs) {681const idx = args.indexOf(a);682if (idx >= 0) {683args.splice(idx, 1);684}685}686}687688const quitListener = () => {689if (!this.relaunchHandler?.handleRelaunch(options)) {690this.trace('Lifecycle#relaunch() - calling app.relaunch()');691electron.app.relaunch({ args });692}693};694electron.app.once('quit', quitListener);695696// `app.relaunch()` does not quit automatically, so we quit first,697// check for vetoes and then relaunch from the `app.on('quit')` event698const veto = await this.quit(true /* will restart */);699if (veto) {700electron.app.removeListener('quit', quitListener);701}702}703704async kill(code?: number): Promise<void> {705this.trace('Lifecycle#kill()');706707// Give main process participants a chance to orderly shutdown708await this.fireOnWillShutdown(ShutdownReason.KILL);709710// From extension tests we have seen issues where calling app.exit()711// with an opened window can lead to native crashes (Linux). As such,712// we should make sure to destroy any opened window before calling713// `app.exit()`.714//715// Note: Electron implements a similar logic here:716// https://github.com/electron/electron/blob/fe5318d753637c3903e23fc1ed1b263025887b6a/spec-main/window-helpers.ts#L5717718await Promise.race([719720// Still do not block more than 1s721timeout(1000),722723// Destroy any opened window: we do not unload windows here because724// there is a chance that the unload is veto'd or long running due725// to a participant within the window. this is not wanted when we726// are asked to kill the application.727(async () => {728for (const window of getAllWindowsExcludingOffscreen()) {729if (window && !window.isDestroyed()) {730let whenWindowClosed: Promise<void>;731if (window.webContents && !window.webContents.isDestroyed()) {732whenWindowClosed = new Promise(resolve => window.once('closed', resolve));733} else {734whenWindowClosed = Promise.resolve();735}736737window.destroy();738await whenWindowClosed;739}740}741})()742]);743744// Now exit either after 1s or all windows destroyed745electron.app.exit(code);746}747}748749750