This repository was archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Goroutines global panic handler #188
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package reflection | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "sync" | ||
| "unsafe" | ||
| _ "unsafe" | ||
|
|
||
| "github.com/undefinedlabs/go-mpatch" | ||
| ) | ||
|
|
||
| var ( | ||
| patchOnPanic *mpatch.Patch | ||
| mOnPanic sync.Mutex | ||
| onPanicHandlers []func(e interface{}) | ||
|
|
||
| patchOnExit *mpatch.Patch | ||
| mOnExit sync.Mutex | ||
| onExitHandler []func(e interface{}) | ||
| ) | ||
|
|
||
| // Adds a global panic handler (this handler will be executed before any recover call) | ||
| func AddPanicHandler(fn func(interface{})) { | ||
| mOnPanic.Lock() | ||
| defer mOnPanic.Unlock() | ||
| if patchOnPanic == nil { | ||
| gp := lgopanic | ||
| np, err := mpatch.PatchMethodByReflect(reflect.Method{Func: reflect.ValueOf(gp)}, gopanic) | ||
| if err == nil { | ||
| patchOnPanic = np | ||
| } | ||
| } | ||
| onPanicHandlers = append(onPanicHandlers, fn) | ||
| } | ||
|
|
||
| // Adds a global panic handler before process kill (this handler will be executed if not recover is set before the process exits) | ||
| func AddOnPanicExitHandler(fn func(interface{})) { | ||
| mOnExit.Lock() | ||
| defer mOnExit.Unlock() | ||
| if patchOnExit == nil { | ||
| gp := lpreprintpanics | ||
| np, err := mpatch.PatchMethodByReflect(reflect.Method{Func: reflect.ValueOf(gp)}, preprintpanics) | ||
| if err == nil { | ||
| patchOnExit = np | ||
| } | ||
| } | ||
| onExitHandler = append(onExitHandler, fn) | ||
| } | ||
|
|
||
| func gopanic(e interface{}) { | ||
| mOnPanic.Lock() | ||
| defer mOnPanic.Unlock() | ||
| for _, fn := range onPanicHandlers { | ||
| fn(e) | ||
| } | ||
| patchOnPanic.Unpatch() | ||
| defer patchOnPanic.Patch() | ||
| lgopanic(e) | ||
| } | ||
|
|
||
| func preprintpanics(p *_panic) { | ||
| mOnExit.Lock() | ||
| defer mOnExit.Unlock() | ||
| for _, fn := range onExitHandler { | ||
| fn(p.arg) | ||
| } | ||
| patchOnExit.Unpatch() | ||
| defer patchOnExit.Patch() | ||
| lpreprintpanics(p) | ||
| } | ||
|
|
||
| //go:linkname lgopanic runtime.gopanic | ||
| func lgopanic(e interface{}) | ||
|
|
||
| //go:linkname lpreprintpanics runtime.preprintpanics | ||
| func lpreprintpanics(p *_panic) | ||
|
|
||
| type _panic struct { | ||
| argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink | ||
| arg interface{} // argument to panic | ||
| link *_panic // link to earlier panic | ||
| recovered bool // whether this panic is over | ||
| aborted bool // the panic was aborted | ||
| } | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package reflection_test | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| _ "go.undefinedlabs.com/scopeagent/autoinstrument" | ||
| "go.undefinedlabs.com/scopeagent/reflection" | ||
| ) | ||
|
|
||
| func TestPanicHandler(t *testing.T) { | ||
| var panicHandlerVisit int32 | ||
|
|
||
| reflection.AddPanicHandler(func(e interface{}) { | ||
| t.Log("PANIC HANDLER FOR:", e) | ||
| atomic.AddInt32(&panicHandlerVisit, 1) | ||
| }) | ||
|
|
||
| t.Run("OnPanic", func(t2 *testing.T) { | ||
| go func() { | ||
|
|
||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| t.Log("PANIC RECOVERED") | ||
| } | ||
| }() | ||
|
|
||
| t.Log("PANICKING!") | ||
| panic("Panic error") | ||
|
|
||
| }() | ||
|
|
||
| time.Sleep(1 * time.Second) | ||
| }) | ||
|
|
||
| if atomic.LoadInt32(&panicHandlerVisit) != 1 { | ||
| t.Fatalf("panic handler should be executed once.") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.