Skip to content

Application Lifecycle

Desktop applications have a lifecycle from startup to shutdown. Wails v3 provides services, events, and hooks to manage this lifecycle effectively.

Diagram

Create your application with application.New():

app := application.New(application.Options{
Name: "My App",
Description: "An application built with Wails",
Services: []application.Service{
application.NewService(&MyService{}),
},
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
})

What happens:

  1. Options are parsed and validated
  2. Services are registered (but not started yet)
  3. Asset server is configured
  4. Runtime is set up

Call app.Run() to start the application:

err := app.Run() // Blocks until quit
if err != nil {
log.Fatal(err)
}

What happens:

  1. Services are started in registration order
  2. Event listeners are activated
  3. Windows can be created
  4. Event loop begins

The application enters the event loop where it spends most of its time:

  • OS events processed (mouse, keyboard, window events)
  • Go-to-JS messages handled
  • JS-to-Go calls executed
  • UI updates rendered

When the application quits:

  1. ShouldQuit callback is checked (if set)
  2. OnShutdown callbacks are executed
  3. Services are shut down in reverse order
  4. Windows are closed
  5. Resources are released

Services are the primary way to manage lifecycle in Wails v3. They provide startup and shutdown hooks through interfaces. For complete documentation on services, see the Services guide.

type MyService struct {
db *sql.DB
}
// ServiceStartup is called when the application starts
func (s *MyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
var err error
s.db, err = sql.Open("sqlite3", "app.db")
if err != nil {
return err // Startup aborts if error returned
}
// Run migrations
if err := s.runMigrations(); err != nil {
return err
}
return nil
}
// ServiceShutdown is called when the application shuts down
func (s *MyService) ServiceShutdown() error {
if s.db != nil {
return s.db.Close()
}
return nil
}
app := application.New(application.Options{
Services: []application.Service{
application.NewService(&MyService{}),
application.NewService(&AnotherService{}),
},
})

Key points:

  • Services start in registration order
  • Services shut down in reverse registration order
  • If a service’s ServiceStartup returns an error, the application aborts
  • The ctx passed to ServiceStartup is cancelled when shutdown begins

The context passed to ServiceStartup is valid for the application’s lifetime:

func (s *MyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
// Start a background task that respects shutdown
go func() {
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.performBackgroundSync()
case <-ctx.Done():
// Application is shutting down
return
}
}
}()
return nil
}

You can also access the context from the application instance:

app := application.Get()
ctx := app.Context()

These are convenience callbacks in application.Options that let you hook into the application lifecycle without creating a full service. They’re useful for simple cleanup tasks, quit confirmation, or when you need to run code at specific points in the shutdown sequence.

For more complex lifecycle management with startup logic, dependency injection, or stateful resources, use Services instead.

The ShouldQuit callback is called whenever a quit is requested—whether by the user closing the last window, pressing Cmd+Q (macOS) / Alt+F4 (Windows), or calling app.Quit() programmatically.

Return value:

  • Return true to allow the quit to proceed (application will shut down)
  • Return false to cancel the quit (application continues running)

This is your opportunity to intercept quit requests and optionally prevent them, for example to prompt the user about unsaved changes:

app := application.New(application.Options{
ShouldQuit: func() bool {
if !hasUnsavedChanges() {
return true // No unsaved changes, allow quit
}
// Prompt the user
result, _ := application.QuestionDialog().
SetTitle("Unsaved Changes").
SetMessage("You have unsaved changes. Quit anyway?").
AddButton("Quit", "quit").
AddButton("Cancel", "cancel").
Show()
// Only quit if user clicked "Quit"
return result == "quit"
},
})

If ShouldQuit is not set, the application will quit immediately when requested.

When ShouldQuit is called:

  • User closes the last window (unless DisableQuitOnLastWindowClosed is set)
  • User presses Cmd+Q on macOS
  • User presses Alt+F4 on Windows (when focused on last window)
  • Code calls app.Quit()

When ShouldQuit is NOT called:

  • The process is killed (SIGKILL, Task Manager force-quit)
  • os.Exit() is called directly

The OnShutdown callback is called when the application is confirmed to be quitting (after ShouldQuit returns true, if set). Use this for cleanup tasks like saving state, closing database connections, or releasing resources.

app := application.New(application.Options{
OnShutdown: func() {
// Save application state
saveState()
// Close connections
cleanup()
},
})

You can also register additional shutdown callbacks programmatically at any time during the application’s lifecycle:

app.OnShutdown(func() {
log.Println("Application shutting down...")
})

Multiple callbacks are executed in the order they were registered. The shutdown process blocks until all callbacks complete.

Important: Keep shutdown callbacks fast (under 1 second). The operating system may force-terminate applications that take too long to quit, which could interrupt your cleanup and cause data loss.

The PostShutdown callback is called after all shutdown tasks have completed, just before the process terminates. At this point, the application instance is no longer usable—windows are closed, services are shut down, and resources are released.

This is primarily useful for:

  • Final logging that must happen after all other cleanup
  • Testing and debugging shutdown behaviour
  • Platforms where app.Run() doesn’t return (the callback ensures your code runs)
app := application.New(application.Options{
PostShutdown: func() {
// Final logging
log.Println("Application terminated cleanly")
// Flush any buffered logs
logger.Sync()
},
})

Note: Do not attempt to use application features (windows, dialogs, etc.) in PostShutdown—they are no longer available.

Wails provides an event system that notifies you when things happen in your application—windows opening, the application starting, theme changes, and more. You can listen to these events to react to lifecycle changes without blocking or intercepting them.

For window events, you can also use RegisterHook instead of OnWindowEvent to intercept and cancel actions—for example, preventing a window from closing. See Window Hooks below.

For full documentation on the event system, see the Events guide.

Listen to application lifecycle events:

app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(event *application.ApplicationEvent) {
app.Logger.Info("Application has started!")
})

Platform-specific events are also available:

// macOS
app.Event.OnApplicationEvent(events.Mac.ApplicationDidFinishLaunching, func(event *application.ApplicationEvent) {
// Handle macOS launch
})
app.Event.OnApplicationEvent(events.Mac.ApplicationWillTerminate, func(event *application.ApplicationEvent) {
// Handle macOS termination
})
// Windows
app.Event.OnApplicationEvent(events.Windows.ApplicationStarted, func(event *application.ApplicationEvent) {
// Handle Windows start
})

Listen to window lifecycle events:

window := app.Window.New()
window.OnWindowEvent(events.Common.WindowFocus, func(e *application.WindowEvent) {
app.Logger.Info("Window gained focus")
})
window.OnWindowEvent(events.Common.WindowClosing, func(e *application.WindowEvent) {
app.Logger.Info("Window is closing")
})

Use RegisterHook instead of OnWindowEvent when you need to cancel an event:

window := app.Window.New()
var countdown = 3
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
countdown--
if countdown > 0 {
app.Logger.Info("Not closing yet!", "remaining", countdown)
e.Cancel() // Prevent the window from closing
return
}
app.Logger.Info("Window closing now")
})

Difference between OnWindowEvent and RegisterHook:

  • OnWindowEvent: Notifies you when an event happens (cannot cancel)
  • RegisterHook: Lets you intercept and potentially cancel the event

Windows have their own lifecycle, from creation through to destruction. Each window loads its frontend content independently and can be shown, hidden, or closed at any time. When a user attempts to close a window, you can intercept this with a RegisterHook to prompt for confirmation or hide the window instead of destroying it.

For complete window documentation, see the Windows guide.

Diagram
window := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "My Window",
Width: 800,
Height: 600,
})
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
if hasUnsavedChanges() {
// Show dialog
result, _ := application.QuestionDialog().
SetTitle("Unsaved Changes").
SetMessage("Save before closing?").
AddButton("Save", "save").
AddButton("Discard", "discard").
AddButton("Cancel", "cancel").
Show()
switch result {
case "save":
saveChanges()
// Allow close
case "cancel":
e.Cancel() // Prevent close
}
// "discard" falls through and allows close
}
})

A common pattern for system tray apps:

window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
window.Hide() // Hide instead of destroy
e.Cancel() // Prevent actual close
})

With multiple windows:

mainWindow := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "Main Window",
})
settingsWindow := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "Settings",
Width: 400,
Height: 600,
Hidden: true, // Start hidden
})

Default behaviour varies by platform:

PlatformDefault when last window closes
macOSApp stays running (menu bar remains)
WindowsApp quits
LinuxApp quits

macOS follows native platform conventions where applications typically remain active in the menu bar even with no windows. Windows and Linux quit by default.

Make all platforms quit when last window closes:

app := application.New(application.Options{
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})

Make all platforms stay running when last window closes:

This is useful for system tray applications or apps that should remain running in the background.

app := application.New(application.Options{
Windows: application.WindowsOptions{
DisableQuitOnLastWindowClosed: true,
},
Linux: application.LinuxOptions{
DisableQuitOnLastWindowClosed: true,
},
})
type DatabaseService struct {
db *sql.DB
}
func (s *DatabaseService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
var err error
s.db, err = sql.Open("sqlite3", "app.db")
if err != nil {
return fmt.Errorf("failed to open database: %w", err)
}
if err := s.db.PingContext(ctx); err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
return nil
}
func (s *DatabaseService) ServiceShutdown() error {
if s.db != nil {
return s.db.Close()
}
return nil
}
// Exported methods are available to the frontend
func (s *DatabaseService) GetUsers() ([]User, error) {
// Query implementation
}
type ConfigService struct {
config *Config
path string
}
func (s *ConfigService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
s.path = "config.json"
data, err := os.ReadFile(s.path)
if err != nil {
if os.IsNotExist(err) {
s.config = &Config{} // Default config
return nil
}
return err
}
return json.Unmarshal(data, &s.config)
}
func (s *ConfigService) ServiceShutdown() error {
data, err := json.MarshalIndent(s.config, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.path, data, 0644)
}
type WorkerService struct {
cancel context.CancelFunc
}
func (s *WorkerService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
workerCtx, cancel := context.WithCancel(ctx)
s.cancel = cancel
go s.runWorker(workerCtx)
return nil
}
func (s *WorkerService) ServiceShutdown() error {
if s.cancel != nil {
s.cancel()
}
return nil
}
func (s *WorkerService) runWorker(ctx context.Context) {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.doWork()
case <-ctx.Done():
return
}
}
}
Hook/InterfaceWhen CalledCan Cancel?Use For
ServiceStartupDuring app.Run(), before event loopNo (return error to abort)Initialisation
ServiceShutdownDuring shutdown, after OnShutdownNoCleanup
OnShutdownWhen quit confirmedNoApplication cleanup
ShouldQuitWhen quit requestedYes (return false)Confirm quit
RegisterHook(WindowClosing)When window close requestedYes (e.Cancel())Prevent window close
OnWindowEventWhen event occursNoReact to events
OnApplicationEventWhen event occursNoReact to events
  • Application menu persists even with no windows
  • Cmd+Q triggers quit (goes through ShouldQuit)
  • Dock icon remains unless hidden
  • Use ApplicationShouldTerminateAfterLastWindowClosed to control quit behaviour
  • No application menu without a window
  • Alt+F4 closes window (can be prevented with RegisterHook)
  • System tray can keep app running
  • Behaviour varies by desktop environment
  • Generally similar to Windows

Causes:

  1. ShouldQuit returning false
  2. OnShutdown taking too long
  3. Background goroutines not stopping

Solution:

// 1. Check ShouldQuit logic
ShouldQuit: func() bool {
log.Println("ShouldQuit called")
return true
}
// 2. Keep OnShutdown fast
OnShutdown: func() {
log.Println("OnShutdown started")
// Fast cleanup only
log.Println("OnShutdown finished")
}
// 3. Use context for background tasks
func (s *MyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
go func() {
<-ctx.Done()
log.Println("Context cancelled, stopping background work")
}()
return nil
}

Solution: Return descriptive errors:

func (s *MyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
if err := s.init(); err != nil {
return fmt.Errorf("failed to initialise: %w", err)
}
return nil
}

The error will be logged and the application will not start.

  • Use services for lifecycle management - They provide proper startup/shutdown hooks
  • Keep shutdown fast - Target under 1 second for all cleanup
  • Use context for cancellation - Stop background tasks properly
  • Handle errors in startup - Return errors to abort cleanly
  • Log lifecycle events - Helps with debugging
  • Don’t block in service startup - Keep initialisation fast (under 2 seconds)
  • Don’t show dialogs in shutdown - App is quitting, UI may not work
  • Don’t ignore the context - Always check ctx.Done() in goroutines
  • Don’t leak resources - Always implement ServiceShutdown

Services - Learn more about the service system Learn More →

Events System - Use events for communication Learn More →

Window Management - Create and manage windows Learn More →


Questions about lifecycle? Ask in Discord or check the examples.