Path: blob/main/src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts
3296 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: boolean = 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.has(windowId)) {436this.windowToCloseRequest.delete(windowId);437438return;439}440441this.trace(`Lifecycle#window.on('close') - window ID ${window.id}`);442443// Otherwise prevent unload and handle it from window444e.preventDefault();445this.unload(window, UnloadReason.CLOSE).then(veto => {446if (veto) {447this.windowToCloseRequest.delete(windowId);448return;449}450451this.windowToCloseRequest.add(windowId);452453// Fire onBeforeCloseWindow before actually closing454this.trace(`Lifecycle#onBeforeCloseWindow.fire() - window ID ${windowId}`);455this._onBeforeCloseWindow.fire(window);456457// No veto, close window now458window.close();459});460}));461windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'closed')(() => {462this.trace(`Lifecycle#window.on('closed') - window ID ${window.id}`);463464// update window count465this.windowCounter--;466467// clear window listeners468windowListeners.dispose();469470// if there are no more code windows opened, fire the onWillShutdown event, unless471// we are on macOS where it is perfectly fine to close the last window and472// the application continues running (unless quit was actually requested)473if (this.windowCounter === 0 && (!isMacintosh || this._quitRequested)) {474this.fireOnWillShutdown(ShutdownReason.QUIT);475}476}));477}478479registerAuxWindow(auxWindow: IAuxiliaryWindow): void {480const win = assertReturnsDefined(auxWindow.win);481482const windowListeners = new DisposableStore();483windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'close')(e => {484this.trace(`Lifecycle#auxWindow.on('close') - window ID ${auxWindow.id}`);485486if (this._quitRequested) {487this.trace(`Lifecycle#auxWindow.on('close') - preventDefault() because quit requested`);488489// When quit is requested, Electron will close all490// auxiliary windows before closing the main windows.491// This prevents us from storing the auxiliary window492// state on shutdown and thus we prevent closing if493// quit is requested.494//495// Interestingly, this will not prevent the application496// from quitting because the auxiliary windows will still497// close once the owning window closes.498499e.preventDefault();500}501}));502windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'closed')(() => {503this.trace(`Lifecycle#auxWindow.on('closed') - window ID ${auxWindow.id}`);504505windowListeners.dispose();506}));507}508509async reload(window: ICodeWindow, cli?: NativeParsedArgs): Promise<void> {510511// Only reload when the window has not vetoed this512const veto = await this.unload(window, UnloadReason.RELOAD);513if (!veto) {514window.reload(cli);515}516}517518unload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {519520// Ensure there is only 1 unload running at the same time521const pendingUnloadPromise = this.mapWindowIdToPendingUnload.get(window.id);522if (pendingUnloadPromise) {523return pendingUnloadPromise;524}525526// Start unload and remember in map until finished527const unloadPromise = this.doUnload(window, reason).finally(() => {528this.mapWindowIdToPendingUnload.delete(window.id);529});530this.mapWindowIdToPendingUnload.set(window.id, unloadPromise);531532return unloadPromise;533}534535private async doUnload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {536537// Always allow to unload a window that is not yet ready538if (!window.isReady) {539return false;540}541542this.trace(`Lifecycle#unload() - window ID ${window.id}`);543544// first ask the window itself if it vetos the unload545const windowUnloadReason = this._quitRequested ? UnloadReason.QUIT : reason;546const veto = await this.onBeforeUnloadWindowInRenderer(window, windowUnloadReason);547if (veto) {548this.trace(`Lifecycle#unload() - veto in renderer (window ID ${window.id})`);549550return this.handleWindowUnloadVeto(veto);551}552553// finally if there are no vetos, unload the renderer554await this.onWillUnloadWindowInRenderer(window, windowUnloadReason);555556return false;557}558559private handleWindowUnloadVeto(veto: boolean): boolean {560if (!veto) {561return false; // no veto562}563564// a veto resolves any pending quit with veto565this.resolvePendingQuitPromise(true /* veto */);566567// a veto resets the pending quit request flag568this._quitRequested = false;569570return true; // veto571}572573private resolvePendingQuitPromise(veto: boolean): void {574if (this.pendingQuitPromiseResolve) {575this.pendingQuitPromiseResolve(veto);576this.pendingQuitPromiseResolve = undefined;577this.pendingQuitPromise = undefined;578}579}580581private onBeforeUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {582return new Promise<boolean>(resolve => {583const oneTimeEventToken = this.oneTimeListenerTokenGenerator++;584const okChannel = `vscode:ok${oneTimeEventToken}`;585const cancelChannel = `vscode:cancel${oneTimeEventToken}`;586587validatedIpcMain.once(okChannel, () => {588resolve(false); // no veto589});590591validatedIpcMain.once(cancelChannel, () => {592resolve(true); // veto593});594595window.send('vscode:onBeforeUnload', { okChannel, cancelChannel, reason });596});597}598599private onWillUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): Promise<void> {600return new Promise<void>(resolve => {601const oneTimeEventToken = this.oneTimeListenerTokenGenerator++;602const replyChannel = `vscode:reply${oneTimeEventToken}`;603604validatedIpcMain.once(replyChannel, () => resolve());605606window.send('vscode:onWillUnload', { replyChannel, reason });607});608}609610quit(willRestart?: boolean): Promise<boolean /* veto */> {611return this.doQuit(willRestart).then(veto => {612if (!veto && willRestart) {613// Windows: we are about to restart and as such we need to restore the original614// current working directory we had on startup to get the exact same startup615// behaviour. As such, we briefly change back to that directory and then when616// Code starts it will set it back to the installation directory again.617try {618if (isWindows) {619const currentWorkingDir = cwd();620if (currentWorkingDir !== process.cwd()) {621process.chdir(currentWorkingDir);622}623}624} catch (err) {625this.logService.error(err);626}627}628629return veto;630});631}632633private doQuit(willRestart?: boolean): Promise<boolean /* veto */> {634this.trace(`Lifecycle#quit() - begin (willRestart: ${willRestart})`);635636if (this.pendingQuitPromise) {637this.trace('Lifecycle#quit() - returning pending quit promise');638639return this.pendingQuitPromise;640}641642// Remember if we are about to restart643if (willRestart) {644this.stateService.setItem(LifecycleMainService.QUIT_AND_RESTART_KEY, true);645}646647this.pendingQuitPromise = new Promise(resolve => {648649// Store as field to access it from a window cancellation650this.pendingQuitPromiseResolve = resolve;651652// Calling app.quit() will trigger the close handlers of each opened window653// and only if no window vetoed the shutdown, we will get the will-quit event654this.trace('Lifecycle#quit() - calling app.quit()');655electron.app.quit();656});657658return this.pendingQuitPromise;659}660661private trace(msg: string): void {662if (this.environmentMainService.args['enable-smoke-test-driver']) {663this.logService.info(msg); // helps diagnose issues with exiting from smoke tests664} else {665this.logService.trace(msg);666}667}668669setRelaunchHandler(handler: IRelaunchHandler): void {670this.relaunchHandler = handler;671}672673async relaunch(options?: IRelaunchOptions): Promise<void> {674this.trace('Lifecycle#relaunch()');675676const args = process.argv.slice(1);677if (options?.addArgs) {678args.push(...options.addArgs);679}680681if (options?.removeArgs) {682for (const a of options.removeArgs) {683const idx = args.indexOf(a);684if (idx >= 0) {685args.splice(idx, 1);686}687}688}689690const quitListener = () => {691if (!this.relaunchHandler?.handleRelaunch(options)) {692this.trace('Lifecycle#relaunch() - calling app.relaunch()');693electron.app.relaunch({ args });694}695};696electron.app.once('quit', quitListener);697698// `app.relaunch()` does not quit automatically, so we quit first,699// check for vetoes and then relaunch from the `app.on('quit')` event700const veto = await this.quit(true /* will restart */);701if (veto) {702electron.app.removeListener('quit', quitListener);703}704}705706async kill(code?: number): Promise<void> {707this.trace('Lifecycle#kill()');708709// Give main process participants a chance to orderly shutdown710await this.fireOnWillShutdown(ShutdownReason.KILL);711712// From extension tests we have seen issues where calling app.exit()713// with an opened window can lead to native crashes (Linux). As such,714// we should make sure to destroy any opened window before calling715// `app.exit()`.716//717// Note: Electron implements a similar logic here:718// https://github.com/electron/electron/blob/fe5318d753637c3903e23fc1ed1b263025887b6a/spec-main/window-helpers.ts#L5719720await Promise.race([721722// Still do not block more than 1s723timeout(1000),724725// Destroy any opened window: we do not unload windows here because726// there is a chance that the unload is veto'd or long running due727// to a participant within the window. this is not wanted when we728// are asked to kill the application.729(async () => {730for (const window of getAllWindowsExcludingOffscreen()) {731if (window && !window.isDestroyed()) {732let whenWindowClosed: Promise<void>;733if (window.webContents && !window.webContents.isDestroyed()) {734whenWindowClosed = new Promise(resolve => window.once('closed', resolve));735} else {736whenWindowClosed = Promise.resolve();737}738739window.destroy();740await whenWindowClosed;741}742}743})()744]);745746// Now exit either after 1s or all windows destroyed747electron.app.exit(code);748}749}750751752