Skip to content

feat: implement centralized panic recovery mechanism#3039

Open
coolwednesday wants to merge 9 commits intodevelopmentfrom
panic-recovery
Open

feat: implement centralized panic recovery mechanism#3039
coolwednesday wants to merge 9 commits intodevelopmentfrom
panic-recovery

Conversation

@coolwednesday
Copy link
Member

@coolwednesday coolwednesday commented Feb 26, 2026

Summary

Resurrects and improves the work from closed PR #2521, introducing a centralized panic recovery mechanism for GoFr.

  • Closes Panic Recovery Across GoFr Features #2500
  • New recovery.go: RecoveryHandler struct with Recover(), RecoverWithCallback(), RecoverWithChannel() methods; SafeGo/SafeGoWithCallback helpers; shared RecoveryLog and ErrPanic sentinel
  • subscriber.go: Replaces ad-hoc defer/recover + local panicLog/panicRecovery with NewRecoveryHandler(...).Recover()
  • cron.go: Replaces inline recover() with RecoverWithCallback — all metrics instrumentation (total/success/failure counters, histogram) is preserved with a panicked flag tracked via the callback
  • gofr.go: Updates Migrate() to use NewRecoveryHandler (removes the TODO comment)
  • cmd.go: Wraps command handler execution with NewRecoveryHandler(...).Recover()
  • run.go: startShutdownHandler and startTelemetryIfEnabled use SafeGo; metrics, HTTP, gRPC, and subscription-manager goroutines get NewRecoveryHandler(...).Recover()
  • handler.go: panicRecoveryHandler uses shared RecoveryLog instead of local panicLog struct

Differences from PR #2521

  • Cron metrics preserved: The original PR removed all histogram/counter instrumentation from cron.go. This PR preserves them using a panicked boolean and RecoverWithCallback.
  • ISSUES.md excluded: The original PR added an internal issues file to the repo root. Not included here.
  • go.work.sum not modified: Only application code is changed.

Test plan

  • go test gofr.dev/pkg/gofr passes (all 13 new recovery tests + all existing tests)
  • TestCronTab_runScheduled_Panic passes — verifies cron panic is logged with new component-based format
  • TestRecoveryHandler_ConcurrentPanics — 10 concurrent goroutines all recover correctly
  • TestSafeGo / TestSafeGoWithCallback — goroutine-level recovery verified
  • Cron metrics (success/failure counters, duration histogram) still recorded on panic via RecoverWithCallback

vipul-rawat and others added 9 commits July 9, 2025 11:53
Introduces RecoveryHandler, a centralized panic recovery mechanism that
replaces scattered ad-hoc defer/recover blocks across the codebase.

- Add recovery.go with RecoveryHandler, RecoveryLog, ErrPanic, SafeGo,
  and SafeGoWithCallback
- Replace inline recover() in subscriber.go with NewRecoveryHandler
- Replace inline recover() in cron.go with RecoverWithCallback, keeping
  all metrics instrumentation (total/success/failure counters, histogram)
- Replace inline recover() in gofr.go (Migrate) with NewRecoveryHandler
- Replace inline recover() in cmd.go with NewRecoveryHandler
- Add SafeGo wrappers to shutdown-handler and telemetry goroutines in run.go
- Add NewRecoveryHandler to metrics, HTTP, gRPC, and subscription goroutines
- Update handler.go to use shared RecoveryLog instead of local panicLog
- Update cron_test.go to assert the new component-based log format
- Add recovery_test.go with 13 tests covering all RecoveryHandler methods

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Merge latest development changes and fix nolintlint/wsl_v5 linter
warnings in recovery.go and cmd.go.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic Recovery Across GoFr Features

2 participants