Skip to content

Commit

Permalink
print a stack trace of panicking goroutines.
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed Jun 1, 2015
1 parent 1b24f9c commit e1ceaf7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
13 changes: 11 additions & 2 deletions chanrpc/chanrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package chanrpc
import (
"errors"
"fmt"
"github.com/name5566/leaf/conf"
"runtime"
)

// one server per goroutine (goroutine not safe)
Expand Down Expand Up @@ -88,8 +90,15 @@ func (s *Server) ret(ci *CallInfo, ri *RetInfo) (err error) {
func (s *Server) Exec(ci *CallInfo) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
s.ret(ci, &RetInfo{err: err})
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
runtime.Stack(buf, false)
err = fmt.Errorf("%v: %s", r, buf)
} else {
err = fmt.Errorf("%v", r)
}

s.ret(ci, &RetInfo{err: fmt.Errorf("%v", r)})
}
}()

Expand Down
3 changes: 3 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package conf

var LenStackBuf = 1024
26 changes: 23 additions & 3 deletions go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package g

import (
"container/list"
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/log"
"runtime"
"sync"
)

Expand Down Expand Up @@ -37,7 +39,13 @@ func (g *Go) Go(f func(), cb func()) {
defer func() {
g.ChanCb <- cb
if r := recover(); r != nil {
log.Error("%v", r)
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
runtime.Stack(buf, false)
log.Error("%v: %s", r, buf)
} else {
log.Error("%v", r)
}
}
}()

Expand All @@ -49,7 +57,13 @@ func (g *Go) Cb(cb func()) {
defer func() {
g.pendingGo--
if r := recover(); r != nil {
log.Error("%v", r)
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
runtime.Stack(buf, false)
log.Error("%v: %s", r, buf)
} else {
log.Error("%v", r)
}
}
}()

Expand Down Expand Up @@ -89,7 +103,13 @@ func (c *LinearContext) Go(f func(), cb func()) {
defer func() {
c.g.ChanCb <- e.cb
if r := recover(); r != nil {
log.Error("%v", r)
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
runtime.Stack(buf, false)
log.Error("%v: %s", r, buf)
} else {
log.Error("%v", r)
}
}
}()

Expand Down
10 changes: 9 additions & 1 deletion module/module.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package module

import (
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/log"
"runtime"
"sync"
)

Expand Down Expand Up @@ -55,7 +57,13 @@ func run(m *module) {
func destroy(m *module) {
defer func() {
if r := recover(); r != nil {
log.Error("%v", r)
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
runtime.Stack(buf, false)
log.Error("%v: %s", r, buf)
} else {
log.Error("%v", r)
}
}
}()

Expand Down
10 changes: 9 additions & 1 deletion timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package timer

import (
"errors"
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/log"
"runtime"
"time"
)

Expand Down Expand Up @@ -32,7 +34,13 @@ func (t *Timer) Cb() {
defer func() {
t.cb = nil
if r := recover(); r != nil {
log.Error("%v", r)
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
runtime.Stack(buf, false)
log.Error("%v: %s", r, buf)
} else {
log.Error("%v", r)
}
}
}()

Expand Down

0 comments on commit e1ceaf7

Please sign in to comment.