Middleware -
func middleware() gin.HandlerFunc {
return func(c *gin.Context) {
//.....
//.....
//.....
//.....
c.Set("someKey", someValue)
log.WithFields(log.Fields{
"someKey": someValue,
}).Infof("Middleware call")
c.Next()
}
}
Some handler that uses the middleware -
func someHandler(c *gin.Context) {
//.....
// Panic is being thrown sometimes
c.MustGet("someKey")
//.....
}
This results (Sometimes) in panic, for no apparent reason as I can clearly see the logging line with the expected values.
time="2017-03-15T08:08:05Z" level=info msg="Middleware call" someKey=someValue
Comment From: itsjamie
Does middleware
run on the same goroutine as someHandler
?
Comment From: tsirolnik
It does
Comment From: tsirolnik
@itsjamie Here's a panic I've caught, seems like the context is shared until the last method -
Key "someKey" does not exist
/usr/lib/go/src/runtime/panic.go:426 (0x487fb9)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/context.go:180 (0x5ceb48)
/home/xxx/xxx/dev/xxx/yyy/src/zzz.go:211 (0x417217)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/context.go:97 (0x5ce44a)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/recovery.go:45 (0x5e1081)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/context.go:97 (0x5ce44a)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/logger.go:72 (0x5e02f2)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/context.go:97 (0x5ce44a)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/gin.go:284 (0x5d5582)
/home/xxx/xxx/dev/golang/src/github.com/gin-gonic/gin/gin.go:265 (0x5d51b7)
/usr/lib/go/src/net/http/server.go:2081 (0x54bdee)
/usr/lib/go/src/net/http/server.go:1472 (0x54869e)
/usr/lib/go/src/runtime/asm_amd64.s:1998 (0x4b8b11)
Comment From: maxatome
@tsirolnik in your stack trace I do not see your middleware closure (in fact the call to c.Next()
like the ones for recovery and logger). It means that in the case of this panic your middleware
closure is not called, so the someKey
key is not set, so MustGet("someKey")
panics.
Note that it is not the same problem as in #702. Here the key is not found in the context instead of concurrent map read and map write for #702.
Comment From: thinkerou
As @maxatome said, if still have problem please reopen, closing.
Comment From: anibal18
Hello, I have the same error ( Key "localizer" does not exist ) and I haven't been able to fix it.
route.Use(ginI18n.Localize(ginI18n.WithBundle(&ginI18n.BundleCfg{
RootPath:"./i18n",
AcceptLanguage: []language.Tag{language.English, language.Spanish},
DefaultLanguage: language.Spanish,
UnmarshalFunc: json.Unmarshal,
FormatBundleFile: "json",
})))
root.GET("/unauthorize", middleware.ValidToken(), global.GetAllOnuUnauthorize)
return func(ctx *gin.Context) {
token_string := ctx.GetHeader("x-access-token")
if token_string == "" {
localizer := ctx.MustGet("localizer").(*i18n.Localizer)
mensaje := localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tokenError"})
ctx.JSON(http.StatusUnauthorized, mensaje)
ctx.Abort()
return
}