Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MustGet sometimes panics even though Set was called in middleware #836

Closed
tsirolnik opened this issue Mar 15, 2017 · 5 comments
Closed

MustGet sometimes panics even though Set was called in middleware #836

tsirolnik opened this issue Mar 15, 2017 · 5 comments

Comments

@tsirolnik
Copy link
Contributor

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

@itsjamie
Copy link
Contributor

Does middleware run on the same goroutine as someHandler?

@tsirolnik
Copy link
Contributor Author

It does

@tsirolnik
Copy link
Contributor Author

tsirolnik commented Mar 19, 2017

@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)

@maxatome
Copy link
Contributor

@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.

@thinkerou
Copy link
Member

As @maxatome said, if still have problem please reopen, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants