Skip to content

Commit

Permalink
opt: use runtime/debug.Stack() to print stack trace of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Mar 23, 2023
1 parent 3110e41 commit 7a56a5c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
7 changes: 2 additions & 5 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package ants

import (
"runtime"
"runtime/debug"
"time"
)

Expand Down Expand Up @@ -53,10 +53,7 @@ func (w *goWorker) run() {
if ph := w.pool.options.PanicHandler; ph != nil {
ph(p)
} else {
w.pool.options.Logger.Printf("worker exits from a panic: %v\n", p)
var buf [4096]byte
n := runtime.Stack(buf[:], false)
w.pool.options.Logger.Printf("worker exits from panic: %s\n", string(buf[:n]))
w.pool.options.Logger.Printf("worker exits from panic: %v\n%s\n", p, debug.Stack())
}
}
// Call Signal() here in case there are goroutines waiting for available workers.
Expand Down
7 changes: 2 additions & 5 deletions worker_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package ants

import (
"runtime"
"runtime/debug"
"time"
)

Expand Down Expand Up @@ -53,10 +53,7 @@ func (w *goWorkerWithFunc) run() {
if ph := w.pool.options.PanicHandler; ph != nil {
ph(p)
} else {
w.pool.options.Logger.Printf("worker with func exits from a panic: %v\n", p)
var buf [4096]byte
n := runtime.Stack(buf[:], false)
w.pool.options.Logger.Printf("worker with func exits from panic: %s\n", string(buf[:n]))
w.pool.options.Logger.Printf("worker exits from panic: %v\n%s\n", p, debug.Stack())
}
}
// Call Signal() here in case there are goroutines waiting for available workers.
Expand Down

0 comments on commit 7a56a5c

Please sign in to comment.