Skip to main content

Lifecycle Service

The Lifecycle Service (ILifecycleService) is a workbench service that manages the application lifecycle, including startup phases, shutdown events, and window lifecycle. It allows components to participate in startup sequencing and handle shutdown gracefully.

Service Overview

The Lifecycle Service is defined in src/vs/workbench/services/lifecycle/common/lifecycle.ts:10 and coordinates the various phases of the application lifecycle.

Service Identifier

Dependency Injection

Lifecycle Phases

VS Code goes through several phases during startup:

Current Phase

Waiting for Phases

Work performed in Starting and Ready phases blocks the editor from showing to the user. Prefer doing work in Restored or Eventually phases when possible.

Startup Kind

The service tracks how the window was started:

Shutdown Events

onBeforeShutdown - Veto Shutdown

Allows components to prevent shutdown from happening:
ShutdownReason
required
The reason for shutdown (CLOSE, QUIT, RELOAD, LOAD)
function
required
Veto function - accepts boolean or Promise and an identifier
Returning a Promise from veto() blocks the shutdown sequence. Try to return synchronously when possible to avoid delays.

onShutdownVeto - Shutdown Prevented

onBeforeShutdownError - Veto Error

onWillShutdown - Join Shutdown Process

Allows components to perform cleanup when shutdown is confirmed:
ShutdownReason
required
The reason for shutdown
CancellationToken
required
Cancellation token that signals if shutdown is being forced
function
required
Join function to add async cleanup operations
function
required
Force shutdown even if join operations haven’t completed

onDidShutdown - After Shutdown

willShutdown Property

Triggering Shutdown

Normally you should not call shutdown() directly. Use IHostService.close() or INativeHostService.quit() instead to properly close windows or quit the application.

Common Use Cases

Deferred Initialization

Saving State on Shutdown

Handling Window Reload

Background Task Scheduling

Cleanup on Shutdown

Best Practices

Defer Heavy Work: Perform expensive initialization in Restored or Eventually phases to avoid blocking the UI.
Synchronous Vetos: Try to return synchronous veto decisions from onBeforeShutdown when possible to avoid delays.
Cancellation Tokens: Check event.token.isCancellationRequested in shutdown handlers to skip optional cleanup if shutdown is forced.
Join Order: Use WillShutdownJoinerOrder.Default unless you specifically need Last and have no service dependencies.
Graceful Degradation: Handle the case where services may already be disposed during Last order joiners.
Unique IDs: Use unique, descriptive IDs for vetos and joiners to help debug slow shutdowns.
  • IHostService - For window management (close, reload)
  • INativeHostService - For native host operations (quit)
  • IStorageService - For persisting state across sessions

See Also