wails/website/docs/reference/runtime/intro.mdx
Lea Anthony 01b661f6a5
feat(v2): add runtime.ResetSignalHandlers() for Linux panic recovery (#4921)
* feat(v2): add runtime.ResetSignalHandlers() for Linux panic recovery

Add a new runtime function that allows users to reset signal handlers
before code that might panic from nil pointer dereferences.

On Linux, WebKit installs signal handlers without the SA_ONSTACK flag,
which prevents Go from properly recovering from panics caused by
SIGSEGV and other signals. This function adds SA_ONSTACK to the
relevant signal handlers (SIGSEGV, SIGBUS, SIGFPE, SIGABRT).

Usage:
```go
go func() {
    defer func() {
        if err := recover(); err != nil {
            log.Printf("Recovered: %v", err)
        }
    }()
    runtime.ResetSignalHandlers()
    // Code that might panic...
}()
```

The function is a no-op on macOS and Windows.

Fixes #3965

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test(v2): add panic-recovery-test example

Add an example that demonstrates the Linux signal handler issue (#3965)
and verifies the fix using runtime.ResetSignalHandlers().

The example includes:
- A Greet function that triggers a nil pointer dereference after a delay
- Auto-call from frontend after 5 seconds
- README with reproduction steps

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 18:55:57 +11:00

143 lines
3.6 KiB
Text

---
sidebar_position: 1
---
# Introduction
The runtime is a library that provides utility methods for your application. There is both a Go and JavaScript runtime
and the aim is to try and keep them at parity where possible.
It has utility methods for:
- [Window](window.mdx)
- [Menu](menu.mdx)
- [Dialog](dialog.mdx)
- [Events](events.mdx)
- [Browser](browser.mdx)
- [Log](log.mdx)
- [Clipboard](clipboard.mdx)
The Go Runtime is available through importing `github.com/wailsapp/wails/v2/pkg/runtime`. All methods in this package
take a context as the first parameter. This context should be obtained from the [OnStartup](../options.mdx#onstartup)
or [OnDomReady](../options.mdx#ondomready) hooks.
:::info Note
Whilst the context will be provided to the
[OnStartup](../options.mdx#onstartup) method, there's no guarantee the runtime will work in this method as
the window is initialising in a different thread. If
you wish to call runtime methods at startup, use [OnDomReady](../options.mdx#ondomready).
:::
The JavaScript library is available to the frontend via the `window.runtime` map. There is a runtime package generated when using `dev`
mode that provides TypeScript declarations for the runtime. This should be located in the `wailsjs` directory in your
frontend directory.
### Hide
Go: `Hide(ctx context.Context)`<br/>
JS: `Hide()`
Hides the application.
:::info Note
On Mac, this will hide the application in the same way as the `Hide` menu item in standard Mac applications.
This is different to hiding the window, but the application still being in the foreground.
For Windows and Linux, this is currently the same as `WindowHide`.
:::
### Show
Shows the application.
:::info Note
On Mac, this will bring the application back into the foreground.
For Windows and Linux, this is currently the same as `WindowShow`.
:::
Go: `Show(ctx context.Context)`<br/>
JS: `Show()`
### Quit
Quits the application.
Go: `Quit(ctx context.Context)`<br/>
JS: `Quit()`
### Environment
Returns details of the current environment.
Go: `Environment(ctx context.Context) EnvironmentInfo`<br/>
JS: `Environment(): Promise<EnvironmentInfo>`
#### EnvironmentInfo
Go:
```go
type EnvironmentInfo struct {
BuildType string
Platform string
Arch string
}
```
JS:
```ts
interface EnvironmentInfo {
buildType: string;
platform: string;
arch: string;
}
```
### ResetSignalHandlers
Resets signal handlers to allow panic recovery from nil pointer dereferences and other memory access violations.
Go: `ResetSignalHandlers()`
:::info Linux Only
This function only has an effect on Linux. On macOS and Windows, it is a no-op.
On Linux, WebKit (used for the webview) may install signal handlers without the `SA_ONSTACK` flag, which prevents
Go from properly recovering from panics caused by nil pointer dereferences (SIGSEGV) or other memory access violations.
Call this function immediately before code that might panic to ensure the signal handlers are properly configured
for Go's panic recovery mechanism.
:::
#### Example
```go
go func() {
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered from panic: %v", err)
}
}()
// Reset signal handlers right before potentially dangerous code
runtime.ResetSignalHandlers()
// Code that might cause a nil pointer dereference...
var t *time.Time
fmt.Println(t.Unix()) // This would normally crash on Linux
}()
```
:::warning
This function must be called in each goroutine where you want panic recovery to work, and should be called
immediately before the code that might panic, as WebKit may reset the signal handlers at any time.
:::