les: protect field access with lock to avoid possible data race #20691
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed inconsistency and also potential data race in les/client_handler.go and les/clientpool.go:
e.g. In les/client_handler.go:
p.headInfo
is read/written 11 times in les/peer.go and les/fetcher.go; 10 out of 11 times it is protected byq.lock.RLock()/Lock()
; 1 out of 11 times it is read without a Lock, which is in funchandle()
on L128.A data race may happen when
handle()
and other func likeannounce()
are called in parallel.The fix is to protect the access to
p.headInfo
and save the result to a local variable.I am not sure if
announce(p, headInfo)
will have an atomicity violation after this fix.A similar bug is an unprotected access to
f.capLimit
insetCapacity()
in les/clientpool.go.I wonder if this function has some special calling guarantee to avoid data race on
f.capLimit
, since all other functions protect the field withf.lock.Lock()
.See #20651