buaazp / fasthttprouter

A high performance fasthttp request router that scales well
http://godoc.org/github.com/buaazp/fasthttprouter
BSD 3-Clause "New" or "Revised" License
876 stars 91 forks source link

missing stack trace in case of panic #23

Closed devghosh closed 7 years ago

devghosh commented 7 years ago
var panicHandler = func(ctx *fasthttp.RequestCtx, p interface{}) {
    // use runtime to get error stack
}
....
....

// intialise router
router = fasthttprouter.New()
router.PanicHandler = panicHandler

If panic has occurred inside my code, then in panicHandler I am not able to see the stack trace where panic started, it shows only fasthttp stack. Anyway to see the original stack ?

razonyang commented 7 years ago

I am using the runtime.Callers to get stack info, hope this helps.

        callers := make([]uintptr, 10)
        num := runtime.Callers(0, callers)
        frames := runtime.CallersFrames(callers)

        var (
            frame runtime.Frame
            more bool
        )
        for {
            frame, more = frames.Next();
            fmt.Println(frame.File, frame.Line)
            num--
            if !more || num <= 0 {
                break
            }
        }
buaazp commented 7 years ago

PanicHandler really need improvements. You can try @ruishengyang 's solution temporarily.

buaazp commented 7 years ago

Hi @devghosh , I test in my codes:

func apiServerPanic(ctx *fasthttp.RequestCtx, rcv interface{}) {
    log.Printf("server panic: %+v\n%s", rcv, debug.Stack())
    msg := fmt.Sprintf("server painc: %v\n", rcv)
    ctx.Write([]byte(msg))
    ctx.Write(debug.Stack())
    ctx.SetStatusCode(500)
    return
}

...
router.PanicHandler = apiServerPanic

And server successfully log and return the right stack:

server painc: runtime error: index out of range
goroutine 6 [running]:
runtime/debug.Stack(0xc420184000, 0xc420016420, 0x30)
    /usr/local/opt/go/libexec/src/runtime/debug/stack.go:24 +0x79
bdn/server.apiServerPanic(0xc420184000, 0x2d0420, 0xc4200140f0)
    /Users/zippo/develop/go/src/bdn/server/server.go:73 +0x1cd
github.com/buaazp/fasthttprouter.(*Router).recv(0xc4201568d0, 0xc420184000)
    /Users/zippo/develop/go/src/github.com/buaazp/fasthttprouter/router.go:236 +0x5b
panic(0x2d0420, 0xc4200140f0)
    /usr/local/opt/go/libexec/src/runtime/panic.go:458 +0x243
bdn/util.FastItWorks(0xc420184000)
    /Users/zippo/develop/go/src/bdn/util/http.go:233 +0x14
github.com/buaazp/fasthttprouter.(*Router).Handler(0xc4201568d0, 0xc420184000)
    /Users/zippo/develop/go/src/github.com/buaazp/fasthttprouter/router.go:300 +0xad5
bdn/util.FastLogger.func1(0xc420184000)
    /Users/zippo/develop/go/src/bdn/util/logger.go:30 +0x20b
github.com/valyala/fasthttp.(*Server).serveConn(0xc420067d40, 0x48c260, 0xc42002e010, 0x1, 0x101)
    /Users/zippo/develop/go/src/github.com/valyala/fasthttp/server.go:1533 +0x573
github.com/valyala/fasthttp.(*Server).(github.com/valyala/fasthttp.serveConn)-fm(0x48c260, 0xc42002e010, 0xc42002bf58, 0x1)
    /Users/zippo/develop/go/src/github.com/valyala/fasthttp/server.go:1250 +0x3e
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc420170080, 0xc42000c400)
    /Users/zippo/develop/go/src/github.com/valyala/fasthttp/workerpool.go:210 +0xde
github.com/valyala/fasthttp.(*workerPool).getCh.func1(0xc420170080, 0xc42000c400, 0x2aa4a0, 0xc42000c400)
    /Users/zippo/develop/go/src/github.com/valyala/fasthttp/workerpool.go:182 +0x35
created by github.com/valyala/fasthttp.(*workerPool).getCh
    /Users/zippo/develop/go/src/github.com/valyala/fasthttp/workerpool.go:184 +0x111

Can you show me the only fasthttp stack you mentioned?

devghosh commented 7 years ago

Hi, @buaazp thanks for the reply, please see the stack trace

runtime/debug.Stack(0x42e660, 0x2f, 0x0)
    /usr/local/go/src/runtime/debug/stack.go:24 +0x79
bitbucket.org/externalproject/session-service/routes.glob..func1(0xc42011ed80, 0x6ffe60, 0xc42000c130)
    /home/project/go/src/github.com/project/myproject/_libs/src/bitbucket.org/externalproject/session-service/routes/route_manager.go:22 +0x34
github.com/buaazp/fasthttprouter.(*Router).recv(0xc420170270, 0xc42011ed80)
    /home/project/go/src/github.com/project/myproject/_libs/src/github.com/buaazp/fasthttprouter/router.go:236 +0x5b
panic(0x6ffe60, 0xc42000c130)
    /usr/local/go/src/runtime/panic.go:458 +0x243
github.com/valyala/fasthttp.(*RequestCtx).SetUserValue(0x0, 0x76dcd7, 0x2, 0x6e87e0, 0xc420149650)
    /home/project/go/src/github.com/project/myproject/_libs/src/github.com/valyala/fasthttp/server.go:461 +0x22
github.com/buaazp/fasthttprouter.(*node).getValue(0xc420015680, 0xc420013127, 0xb, 0x0, 0xc4200feb20, 0x7300000000)
    /home/project/go/src/github.com/project/myproject/_libs/src/github.com/buaazp/fasthttprouter/tree.go:364 +0x1c7
github.com/buaazp/fasthttprouter.(*Router).allowed(0xc420170270, 0xc420013100, 0x32, 0xc42003fc48, 0x3, 0x25, 0x33)
    /home/project/go/src/github.com/project/myproject/_libs/src/github.com/buaazp/fasthttprouter/router.go:273 +0x13d
github.com/buaazp/fasthttprouter.(*Router).Handler(0xc420170270, 0xc42011ed80)
    /home/project/go/src/github.com/project/myproject/_libs/src/github.com/buaazp/fasthttprouter/router.go:353 +0x23e
github.com/buaazp/fasthttprouter.(*Router).Handler-fm(0xc42011ed80)

Note: I have used externalproject and fasthttp both as dependency in project

Panic started at some function f1() but stack trace shows only two stack line of my project and it does not contain file containing f1()

bitbucket.org/externalproject/session-service/routes.glob..func1(0xc42011ed80, 0x6ffe60, 0xc42000c130)
    /home/project/go/src/github.com/project/myproject/_libs/src/bitbucket.org/externalproject/session-service/routes/route_manager.go:22 +0x34
buaazp commented 7 years ago

Maybe your fasthttprouter code is not latest. Pull the latest codes in master branch and try again.