cloudwego / hertz

Go HTTP framework with high-performance and strong-extensibility for building micro-services.
https://www.cloudwego.io
Apache License 2.0
5.04k stars 491 forks source link

hertz panic when edit `ctx.Params` on HandleFunc #1149

Closed wzekin closed 1 month ago

wzekin commented 1 month ago

Describe the bug hertz panic when edit ctx.Params on HandleFunc

To Reproduce

  1. New a hertz server like this:
    
    package main

import ( "context" "fmt"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/route/param"

)

func main() { r := server.Default() r.GET("/:a/:b/:c", func(c context.Context, ctx *app.RequestContext) { ctx.Params = make([]param.Param, 1) fmt.Println(1234567) ctx.String(200, "hello") })

r.Spin()

}

2. Execute `curl http://127.0.0.1:8888/a/b/c` three or four times
3. Then, hertz will panic:
```go
2024/07/08 19:12:14.937008 logger.go:190: [Error] GOPOOL: panic in pool: gopool.DefaultPool: runtime error: slice bounds out of range [:2] with capacity 1: goroutine 16 [running]:
runtime/debug.Stack()
        /Users/zekin/.go/src/runtime/debug/stack.go:24 +0x64
github.com/bytedance/gopkg/util/gopool.(*worker).run.func1.1.1()
        /Users/zekin/go/pkg/mod/github.com/bytedance/gopkg@v0.0.0-20240419070415-fefc805d4d2a/util/gopool/worker.go:64 +0x80
panic({0x10336f820?, 0x140004322e8?})
        /Users/zekin/.go/src/runtime/panic.go:770 +0x124
github.com/cloudwego/hertz/pkg/route.(*router).find(0x1400046e988?, {0x140004369a6, 0x6}, 0x1400027eb08, 0x0)
        /Users/zekin/go/pkg/mod/github.com/cloudwego/hertz@v0.9.0/pkg/route/tree.go:410 +0x750
github.com/cloudwego/hertz/pkg/route.(*Engine).ServeHTTP(0x14000176008, {0x1033a9380, 0x10375de40}, 0x1400027e608)
        /Users/zekin/go/pkg/mod/github.com/cloudwego/hertz@v0.9.0/pkg/route/engine.go:760 +0x45c
github.com/cloudwego/hertz/pkg/protocol/http1.Server.Serve({{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x400000, 0x29e8d60800, ...}, ...}, ...)
        /Users/zekin/go/pkg/mod/github.com/cloudwego/hertz@v0.9.0/pkg/protocol/http1/server.go:297 +0xabc
github.com/cloudwego/hertz/pkg/route.(*Engine).Serve(0x14000176008, {0x1033a9380, 0x10375de40}, {0x1033af7a8, 0x14000604470})
        /Users/zekin/go/pkg/mod/github.com/cloudwego/hertz@v0.9.0/pkg/route/engine.go:539 +0x430
github.com/cloudwego/hertz/pkg/route.(*Engine).onData(0x14000176008?, {0x1033a9380?, 0x10375de40?}, {0x1033882e0?, 0x14000604470?})
        /Users/zekin/go/pkg/mod/github.com/cloudwego/hertz@v0.9.0/pkg/route/engine.go:430 +0xe8
github.com/cloudwego/hertz/pkg/network/netpoll.(*transporter).ListenAndServe.func3({0x1033a9380, 0x10375de40}, {0x1033af1e8, 0x1400042de00})
        /Users/zekin/go/pkg/mod/github.com/cloudwego/hertz@v0.9.0/pkg/network/netpoll/transport.go:107 +0xf0
github.com/cloudwego/netpoll.(*connection).onRequest.func2(0x0?)
        /Users/zekin/go/pkg/mod/github.com/cloudwego/netpoll@v0.6.0/connection_onevent.go:220 +0x3c
github.com/cloudwego/netpoll.(*connection).onProcess.func1()
        /Users/zekin/go/pkg/mod/github.com/cloudwego/netpoll@v0.6.0/connection_onevent.go:255 +0x9c
github.com/bytedance/gopkg/util/gopool.(*worker).run.func1.1(0x0?, 0x0?)
        /Users/zekin/go/pkg/mod/github.com/bytedance/gopkg@v0.0.0-20240419070415-fefc805d4d2a/util/gopool/worker.go:69 +0x58
github.com/bytedance/gopkg/util/gopool.(*worker).run.func1()
        /Users/zekin/go/pkg/mod/github.com/bytedance/gopkg@v0.0.0-20240419070415-fefc805d4d2a/util/gopool/worker.go:70 +0x18c
created by github.com/bytedance/gopkg/util/gopool.(*worker).run in goroutine 39
        /Users/zekin/go/pkg/mod/github.com/bytedance/gopkg@v0.0.0-20240419070415-fefc805d4d2a/util/gopool/worker.go:41 +0x5c

Expected behavior

hertz processes requests normally

Hertz version:

v0.9.0

Environment:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/zekin/Library/Caches/go-build'
GOENV='/Users/zekin/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/zekin/go/pkg/mod'
GONOPROXY='*.everphoto.cn,git.smartisan.com'
GONOSUMDB='*.everphoto.cn,git.smartisan.com'
GOOS='darwin'
GOPATH='/Users/zekin/go'
GOPRIVATE='*.everphoto.cn,git.smartisan.com'
GOPROXY='https://goproxy.cn|https://proxy.golang.org|direct'
GOROOT='/Users/zekin/.go'
GOSUMDB='sum.golang.google.cn'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/zekin/.go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/zekin/code/test/bbb/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/c7/ywjd9qkj1k9cvhw4tnh266380000gn/T/go-build1287891424=/tmp/go-build -gno-record-gcc-switches -fno-common'

Additional context

Add any other context about the problem here.