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

common/lru: fix race in lru #26164

Merged
merged 1 commit into from
Nov 11, 2022
Merged

common/lru: fix race in lru #26164

merged 1 commit into from
Nov 11, 2022

Conversation

holiman
Copy link
Contributor

@holiman holiman commented Nov 11, 2022

This fixes a problem in the SizeConstrainedLRU. The SCLRU uses an underlying simple lru which is not thread safe.
During the Get operation, the recentness of the accessed item is updated, so it is not a pure read-operation. Therefore, the mutex we need is a full mutex, not RLock.

This PR changes the mutex to be a regular Mutex, instead of RWMutex, so a reviewer can at a glance see that all affected locations are fixed.

Fixes: #26163

@holiman holiman added this to the 1.11.0 milestone Nov 11, 2022
Copy link
Member

@MariusVanDerWijden MariusVanDerWijden left a comment

Choose a reason for hiding this comment

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

LGTM

@holiman holiman merged commit 8334b5f into ethereum:master Nov 11, 2022
yperbasis added a commit to ledgerwatch/erigon-lib that referenced this pull request Nov 14, 2022
[simplelru](https://github.com/hashicorp/golang-lru/tree/master/simplelru)
is not thread safe. During the `Get` operation, the recentness of the
accessed item is updated, so it is not a pure read-operation. Therefore,
the mutex we need to protect the LRUs in txpool is a full mutex, not
`RLock`. See
erigontech/erigon#4679 (comment)
and ethereum/go-ethereum#26164.

Also, RWMutex has a performance overhead compared with the vanilla one
(see, for example, golang/go#38813).

Kudos to Martin Swende for pointing to the issue.
lochjin pushed a commit to lochjin/go-ethereum that referenced this pull request Nov 19, 2022
This fixes a problem in the SizeConstrainedLRU. The SCLRU uses an underlying simple lru which is not thread safe.
During the Get operation, the recentness of the accessed item is updated, so it is not a pure read-operation. Therefore, the mutex we need is a full mutex, not RLock.

This PR changes the mutex to be a regular Mutex, instead of RWMutex, so a reviewer can at a glance see that all affected locations are fixed.

(cherry picked from commit 8334b5f)
dindinw added a commit to Qitmeer/go-ethereum that referenced this pull request Nov 19, 2022
lochjin pushed a commit to lochjin/go-ethereum that referenced this pull request Nov 30, 2022
This fixes a problem in the SizeConstrainedLRU. The SCLRU uses an underlying simple lru which is not thread safe.
During the Get operation, the recentness of the accessed item is updated, so it is not a pure read-operation. Therefore, the mutex we need is a full mutex, not RLock.

This PR changes the mutex to be a regular Mutex, instead of RWMutex, so a reviewer can at a glance see that all affected locations are fixed.

(cherry picked from commit 8334b5f)
shekhirin pushed a commit to shekhirin/go-ethereum that referenced this pull request Jun 6, 2023
This fixes a problem in the SizeConstrainedLRU. The SCLRU uses an underlying simple lru which is not thread safe.
During the Get operation, the recentness of the accessed item is updated, so it is not a pure read-operation. Therefore, the mutex we need is a full mutex, not RLock.

This PR changes the mutex to be a regular Mutex, instead of RWMutex, so a reviewer can at a glance see that all affected locations are 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

Successfully merging this pull request may close these issues.

panic: interface conversion: interface {} is nil, not *simplelru.entry
2 participants