antchfx / htmlquery

htmlquery is golang XPath package for HTML query.
https://github.com/antchfx/xpath
MIT License
727 stars 73 forks source link

Inserting to cache is not thread-safe #19

Closed tamasd closed 4 years ago

tamasd commented 4 years ago

I have an application that processes HTML files, heavily parallelized.

I often get panics because the LRU cache tries to add entries to a map at the same time. The offending line is this one: https://github.com/antchfx/htmlquery/blob/master/cache.go#L37

Guarding this with a mutex would solve my issue.

zhengchun commented 4 years ago

@tamasd , I had submitted Mutex operation, could you test it can works?

zhengchun commented 4 years ago

Closed, fixed in Release 1.2.1.

tamasd commented 4 years ago

Sorry for the late answer. Unfortunately, I still get this issue with 1.2.1. I get data race errors from tests and invalid results.

zhengchun commented 4 years ago

groupcache/lru#Get needs to manipulate its list to track recently used item. So remove read-only lock and used write lock.

zhengchun commented 4 years ago

fixed in v1.2.2

tamasd commented 4 years ago

My tests have passed, the bug looks fixed. Thanks!