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

solve panic. #163

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func (c *Cache) RemoveOldest() {

func (c *Cache) removeElement(e *list.Element) {
c.ll.Remove(e)
kv := e.Value.(*entry)
kv, ok := e.Value.(*entry)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the wrong place for a fix. (Why is removeElement being called on an element with an invalid entry in the first place?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any ideas?
my code just like this:

func A(){
    GlobalMutex.Lock()
    lru.Add(key, value)
    GlobalMutex.Unlock()
}

func B(){
   GlobalMutex.RLock()
   lru.Get(key)
   GlobalMutex.RUnlock()
}
// A and B may be called in multi-goroutines.

only Add() and Get() be called. From the source code, I didn't find out any problem. But it do paniced.

if !ok {
return
}
delete(c.cache, kv.key)
if c.OnEvicted != nil {
c.OnEvicted(kv.key, kv.value)
Expand Down