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

Bug report: item leak when c.promotables is busy #64

Closed
chenqiuhao1997 opened this issue Jun 3, 2021 · 1 comment
Closed

Bug report: item leak when c.promotables is busy #64

chenqiuhao1997 opened this issue Jun 3, 2021 · 1 comment

Comments

@chenqiuhao1997
Copy link

When setting a new key, Cache would use c.promote to add new item in c.list. But when c.promotables is full, c.promote would do nothing, which means new item would not be added into c.list.
That would cause item leak until a get operation when c.promotables is not full. If there is no operation about this key in the future before c.promote successfully takes effect, the memory of the item would never be released because of the reference from the map.

// https://github.com/karlseguin/ccache/blob/master/cache.go

// Set the value in the cache for the specified duration
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
	c.set(key, value, duration, false)
}

func (c *Cache) set(key string, value interface{}, duration time.Duration, track bool) *Item {
	item, existing := c.bucket(key).set(key, value, duration, track)
	if existing != nil {
		c.deletables <- existing
	}
	c.promote(item)
	return item
}

func (c *Cache) promote(item *Item) {
	select {
	case c.promotables <- item:
	default:
	}

}

@chenqiuhao1997 chenqiuhao1997 changed the title item leak when c.promotables is busy Bug report: item leak when c.promotables is busy Jun 3, 2021
karlseguin added a commit that referenced this issue Jun 13, 2021
It's fine to conditionally promote on Get, to avoid blocking on a get
(see: #52) but a set _must_
promote else we can end with an entry in our buckets that isn't in our
list.

issue: #64
@karlseguin
Copy link
Owner

Thanks, you're right. Fixed

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

2 participants