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

runtime error: invalid memory address or nil pointer dereference #30

Open
distravantari opened this issue Aug 17, 2017 · 4 comments
Open
Labels

Comments

@distravantari
Copy link

I try ro copy and paste your example for rivescript session using redis
screen shot 2017-08-17 at 8 13 02 pm

but I keep on getting this error.
screen shot 2017-08-17 at 8 14 13 pm

@kirsle kirsle added the bug label Aug 24, 2017
@kirsle
Copy link
Member

kirsle commented Aug 24, 2017

Notes to self or whoever wants to tackle this one:

  • The line where the panic starts attempted to get the history array from the session driver, but it got a nil slice instead.
  • defaultSession() in the Redis driver is where the history object is supposed to get initialized so it's not nil
  • Init(username) is what calls defaultSession() to make sure a username has a valid session object.
    • Init() is called by all the methods that set session data (set variables, add history, etc.); but it is not called by the getter functions; instead the getter functions should return an error that the username wasn't found in Redis at all.

The fix would be: handle the error here. Actually it would probably be better to move the GetHistory() call a little higher up in the code and skip dealing with any of the %Previous checks if the user has no history yet.

	// See if there were any %Previous's in this topic, or any topic related to
	// it. This should only be done the first time -- not during a recursive
	// redirection. This is because in a redirection, "lastReply" is still gonna
	// be the same as it was the first time, resulting in an infinite loop!
+	history, err := rs.sessions.GetHistory(username)
-	if step == 0 {
+	if err != nil && step == 0 {
		allTopics := []string{topic}
		if len(rs.includes[topic]) > 0 || len(rs.inherits[topic]) > 0 {
			// Get ALL the topics!
			allTopics = rs.getTopicTree(topic, 0)
		}

@julianfrank
Copy link

Is this corrected in the current release? Or there a work around?

@kirsle
Copy link
Member

kirsle commented Aug 30, 2017

Not yet.

As a workaround, if you set a user variable before getting a reply, then it will initialize the user's session and it won't raise a nil pointer exception.

This might be a good idea to do anyway, for example to set an "is_admin" variable that you could query from within RiveScript -- if your bot has any need of such things, anyway.

Example:

// Setting a user variable to initialize their session
// will avoid the nil pointer exception when getting
// their history back from the session manager.
bot.SetUservar(username, "is_admin", isAdmin(username))
reply, err := bot.Reply(username, message)

@julianfrank
Copy link

I get this panic even after setting uservar :(

http: panic serving 127.0.0.1:52478: assignment to entry in nil map
goroutine 13 [running]:
net/http.(*conn).serve.func1(0xc4200f34a0)
        /usr/local/go/src/net/http/server.go:1721 +0xd0
panic(0x8bbaa0, 0xc4202fecc0)
        /usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/aichaos/rivescript-go/sessions/memory.(*MemoryStore).GetAll(0xc42017aa60, 0x0)
        /home/jf/go/src/github.com/aichaos/rivescript-go/sessions/memory/memory.go:106 +0x124
github.com/aichaos/rivescript-go.(*RiveScript).GetAllUservars(0xc4201ca0e0, 0xc4202dc99f)
        /home/jf/go/src/github.com/aichaos/rivescript-go/config.go:300 +0x34
main.RawIntentHandler(0xbb1940, 0xc4201ca380, 0xc42000b400)
        <Panic happens here> .go:57 +0x422
net/http.HandlerFunc.ServeHTTP(0x94fbe0, 0xbb1940, 0xc4201ca380, 0xc42000b400)
        /usr/local/go/src/net/http/server.go:1942 +0x44
main.Logger.func1(0xbb1940, 0xc4201ca380, 0xc42000b400)
<srcdir>/src/routehandlers.go:19 +0xcb
net/http.HandlerFunc.ServeHTTP(0xc4202de040, 0xbb1940, 0xc4201ca380, 0xc42000b400)
        /usr/local/go/src/net/http/server.go:1942 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc4201b1770, 0xbb1940, 0xc4201ca380, 0xc42000b400)
        /home/jf/go/src/github.com/gorilla/mux/mux.go:114 +0x10c
net/http.serverHandler.ServeHTTP(0xc42018c160, 0xbb1940, 0xc4201ca380, 0xc42000b200)
        /usr/local/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc4200f34a0, 0xbb2180, 0xc4202de280)
        /usr/local/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2668 +0x2ce

if I try to

swceBot.SetUservar(Username, "username", Username)
userVars, _ := swceBot.GetUservars(Username)
line 57 => x := swceBot.GetAllUservars()

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

No branches or pull requests

3 participants