safego

package
v1.3.130 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package safego provides goroutine launchers with built-in panic recovery.

A panic in any goroutine that is not handled within that goroutine will crash the entire process, which for a long-running interactive TUI is the worst possible failure mode (the user loses unsaved input, in-flight LLM streams, IM connections, etc.). Wrapping each goroutine in safego.Go ensures that bugs in one subsystem don't take the whole agent down.

The recovered panic is logged via internal/debug, including a full stack trace, so the bug remains diagnosable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPanicHook added in v1.3.120

func GetPanicHook() func(name string, recovered any, stack []byte)

GetPanicHook returns the current panic hook (may be nil).

func Go

func Go(name string, fn func())

Go launches fn in a new goroutine with panic recovery. The name is used only in log output to identify the goroutine in case of a panic.

Use this for any goroutine whose failure should not be allowed to crash the process: stream readers, batch flushers, IM listeners, background fetchers, watchdogs, etc.

func Recover

func Recover(name string)

Recover is a deferred-friendly helper. Call as `defer safego.Recover("name")` at the top of any goroutine body you want to protect. It logs the panic via internal/debug (with full stack) and invokes PanicHook if set, then swallows the panic so the goroutine exits cleanly.

func Run

func Run(name string, fn func())

Run invokes fn synchronously in the current goroutine with panic recovery. Useful when wrapping the body of a goroutine you've already launched yourself, or when running code in main that must not crash.

func SetLogger added in v1.1.81

func SetLogger(fn func(category, format string, args ...any))

SetLogger installs the debug log function. Called once at startup by internal/debug to wire up the dependency without creating an import cycle.

func SetPanicHook added in v1.3.120

func SetPanicHook(fn func(name string, recovered any, stack []byte))

SetPanicHook sets the global panic hook. Pass nil to clear it.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL