Go1.19.5结合Gin返回函数变量时出现阻塞问题求助
Go函数返回时阻塞问题排查
环境信息
Go版本
$ go version go version go1.19.5 linux/amd64 using golang:1.19.5-bullseye on ubuntu:20.04
操作系统与处理器架构(go env输出)
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19.5" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/code/mlflow/go.mod" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4126953061=/tmp/go-build -gno-record-gcc-switches"
问题场景
返回函数变量时出现阻塞,但其他返回路径正常,不确定是否为Go Bug,特此求助。
相关代码如下:
apis.GET("/:ns/workspaces", middle.LoginRequired, route.GH(s.listWorkspaces)) ... func GH(fn func(c *gin.Context) http.HandlerFunc) gin.HandlerFunc { return func(c *gin.Context) { logrus.Info("processing handler") replyer := fn(c) logrus.Info("replyer processing") replyer(c.Writer, c.Request) logrus.Info("replyer done") } } // 处理器: func (s *Server) listWorkspaces(c *gin.Context) http.HandlerFunc { //... cnn, err := s.Conn(ctx) logrus.Info(1) // err = merr.Wrap(nil, 2333, "ops...") if err != nil { return route.Err(err) } defer cnn.Close() logrus.Info(2) items, total, err := models.SearchWorkspaces(ctx, cnn, *searchUnity) err = merr.Wrap(nil, 2333, "ops...") if err != nil { logrus.Info("2.1, err: %#v", err) replyer := route.Err(err) return replyer } logrus.Info(3) } func Err(err error) http.HandlerFunc { logrus.Infof("at reply-Wrapper-Err, err: %#v", err) replyer := func(w http.ResponseWriter, r *http.Request) { logrus.Infof("in replyer...") //... responser(w, r) } logrus.Infof("reply-Wrapper-Err end") return replyer }
预期结果
代码应正常执行、报错或触发panic,日志按预期顺序输出。
实际结果
代码总是在return replyer行处阻塞,但其他返回路径正常。取消注释第一处return route.Err(err)行之前的err = merr.Wrap(nil, 2333, "ops...")时,代码运行正常。使用dlv调试时也会卡住。
内容的提问来源于stack exchange,提问作者zhyokang
相关产品推荐
相关产品推荐

