mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
* 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>
|
||
|---|---|---|
| .. | ||
| frontend | ||
| app.go | ||
| go.mod | ||
| main.go | ||
| README.md | ||
| wails.json | ||
Panic Recovery Test
This example demonstrates the Linux signal handler issue (#3965) and verifies the fix using runtime.ResetSignalHandlers().
The Problem
On Linux, WebKit installs signal handlers without the SA_ONSTACK flag, which prevents Go from recovering panics caused by nil pointer dereferences (SIGSEGV). Without the fix, the application crashes with:
signal 11 received but handler not on signal stack
fatal error: non-Go code set up signal handler without SA_ONSTACK flag
The Solution
Call runtime.ResetSignalHandlers() immediately before code that might panic:
import "github.com/wailsapp/wails/v2/pkg/runtime"
go func() {
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered: %v", err)
}
}()
runtime.ResetSignalHandlers()
// Code that might panic...
}()
How to Reproduce
Prerequisites
- Linux with WebKit2GTK 4.1 installed
- Go 1.21+
- Wails CLI
Steps
-
Build the example:
cd v2/examples/panic-recovery-test wails build -tags webkit2_41 -
Run the application:
./build/bin/panic-recovery-test -
Wait ~10 seconds (the app auto-calls
Greetafter 5s, then waits another 5s before the nil pointer dereference)
Expected Result (with fix)
The panic is recovered and you see:
------------------------------"invalid memory address or nil pointer dereference"
The application continues running.
Without the fix
Comment out the runtime.ResetSignalHandlers() call in app.go and rebuild. The application will crash with a fatal signal 11 error.
Files
app.go- Contains theGreetfunction that demonstrates panic recoveryfrontend/src/main.js- Auto-callsGreetafter 5 seconds to trigger the test
Related
- Issue: https://github.com/wailsapp/wails/issues/3965
- Original fix PR: https://github.com/wailsapp/wails/pull/2152