go-macaron / session

Package session is a middleware that provides the session management of Macaron.
Apache License 2.0
26 stars 27 forks source link

http: panic serving [::1]:61196: session(start): gob: name not registered for interface: #9

Closed aacanakin closed 9 years ago

aacanakin commented 9 years ago

Hello, When I start the web server using macaron, if the session store is empty, there is no problem in setting structs to user; Here's the model;

type User struct {
    Model
    Name     string `json:"name,omitempty"`
    Email    string `json:"email,omitempty"`
    Password string `json:"password,omitempty"`
    Birthday string `json:"birthday,omitempty"`
    Domain   Domain `json:"domain,omitempty"`
}

When I first run sess.Set("user", user) it saves with no errors. However, If I close the web server and open again sess.Set() it shows the following error;

http: panic serving [::1]:61196: session(start): gob: name not registered for interface:

It seems like I need to register some kind of struct metadata to gob. This issue is present whether I use folder session store or mysql.

Are there any solutions to that?

unknwon commented 9 years ago

Thanks your feedback!

But the error seems incomplete?

http: panic serving [::1]:61196: session(start): gob: name not registered for interface:

Does the : actually the last character or you didn't copy the error entirely?

aacanakin commented 9 years ago

After :, there exists the model path of user model

unknwon commented 9 years ago

I need details please, that may help!

aacanakin commented 9 years ago

Ok. Here's the full dump

http: panic serving [::1]:63107: session(start): gob: name not registered for interface: "_/Users/aacanakin/Desktop/dev/gocode/src/project/models.User"
goroutine 10 [running]:
net/http.func·011()
    /usr/local/Cellar/go/1.4.2/libexec/src/net/http/server.go:1130 +0xbb
github.com/macaron-contrib/session.func·004(0xc20805d200)
    /Users/aacanakin/Desktop/dev/gocode/src/github.com/macaron-contrib/session/session.go:158 +0x14d
reflect.Value.call(0x41a860, 0xc2080ebe60, 0x13, 0x582220, 0x4, 0xc20800b620, 0x1, 0x1, 0x0, 0x0, ...)
    /usr/local/Cellar/go/1.4.2/libexec/src/reflect/value.go:419 +0x10e5
reflect.Value.Call(0x41a860, 0xc2080ebe60, 0x13, 0xc20800b620, 0x1, 0x1, 0x0, 0x0, 0x0)
    /usr/local/Cellar/go/1.4.2/libexec/src/reflect/value.go:296 +0xbc
github.com/Unknwon/macaron/inject.(*injector).Invoke(0xc20800b540, 0x41a860, 0xc2080ebe60, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/aacanakin/Desktop/dev/gocode/src/github.com/Unknwon/macaron/inject/inject.go:102 +0x3c7
github.com/Unknwon/macaron.(*Context).run(0xc20805d200)
    /Users/aacanakin/Desktop/dev/gocode/src/github.com/Unknwon/macaron/context.go:113 +0x95
github.com/Unknwon/macaron.func·015(0x938c30, 0xc208045720, 0xc20801fd40, 0xc20803dd70)
    /Users/aacanakin/Desktop/dev/gocode/src/github.com/Unknwon/macaron/router.go:183 +0x3ec
github.com/Unknwon/macaron.(*Router).ServeHTTP(0xc208041f90, 0x938c30, 0xc208045720, 0xc20801fd40)
    /Users/aacanakin/Desktop/dev/gocode/src/github.com/Unknwon/macaron/router.go:271 +0x1ab
github.com/Unknwon/macaron.(*Macaron).ServeHTTP(0xc2080ec0e0, 0x938c30, 0xc208045720, 0xc20801fd40)
    /Users/aacanakin/Desktop/dev/gocode/src/github.com/Unknwon/macaron/macaron.go:171 +0x17c
net/http.serverHandler.ServeHTTP(0xc20805b2c0, 0x938c30, 0xc208045720, 0xc20801fd40)
    /usr/local/Cellar/go/1.4.2/libexec/src/net/http/server.go:1703 +0x19a
net/http.(*conn).serve(0xc2080455e0)
    /usr/local/Cellar/go/1.4.2/libexec/src/net/http/server.go:1204 +0xb57
created by net/http.(*Server).Serve
    /usr/local/Cellar/go/1.4.2/libexec/src/net/http/server.go:1751 +0x35e

This happens if I don't cleanup sessions folder and start the web server

unknwon commented 9 years ago

Hi @aacanakin , thanks your info! I've pushed some changes, please use go get -u github.com/macaron-contrib/session and rebuild your program see if problem is being solved!

aacanakin commented 9 years ago

Hi @Unknwon. The problem continues.

aacanakin commented 9 years ago

I looked at your commit. Before starting the app and registering the session middleware, If I add the following line; the problem is fixed;

gob.Register(models.User{})

But I think this is not an out of the box solution

unknwon commented 9 years ago

@aacanakin thanks for the tip!

unknwon commented 9 years ago

Did some researching, it seems no way to know what types are inside gob encoded data and register automatically.

In your case and other similar cases, I think user have to do:

func init() {
    gob.Register(&models.User{})
}

This way, gob knows how decode it.

aacanakin commented 9 years ago

Ok then, I'm closing this. Thanks.

unknwon commented 9 years ago

@aacanakin sorry about it, maybe someday comes up a better way to encode data into binary, or should just store user ID in session and it's fast to read user info by ID from database.