fix(rpc): guard RCManager subscriptions map and Info reads with the existing mutex#456
Open
amathxbt wants to merge 1 commit into
Open
fix(rpc): guard RCManager subscriptions map and Info reads with the existing mutex#456amathxbt wants to merge 1 commit into
amathxbt wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bug
RCManager(incmd/rpc/sock.go) protects itssubscriptions map[uint64]*RCSubscriptionon the write side —AddSubscription()andRemoveSubscription()both correctly taker.l.Lock()before mutating the map — but nearly every read site accessed the map, and each subscription's mutableInfo *lib.RootChainInfofield, without holding the lock:The same unguarded pattern appeared in
GetValidatorSet,GetOrders,GetOrder,IsValidDoubleSigner,GetMinimumEvidenceHeight,GetCheckpoint,GetLotteryWinner,Transaction, andGetDexBatch.sub.Infois itself reassigned under lock elsewhere (GetRootChainInfodoessub.Info = infowhile holdingr.l), and the websocket read-loop can add/remove subscriptions concurrently with these RPC-handler reads — this is a genuine, reachable Go data race since RPC handlers run concurrently per-request.Fix
Every read of
r.subscriptionsnow takesr.l.Lock()/Unlock()around the map lookup. For functions that also read fields offsub.Info(GetHeight,GetValidatorSet,GetOrders,GetLotteryWinner), the relevantInfofields are snapshotted into local variables while still holding the lock, then the lock is released before any remote RPC call (e.g.sub.ValidatorSet(...),sub.Orders(...)) is made — this closes the race without holding the mutex across network I/O, preserving the existing performance/concurrency characteristics for remote calls.Testing
Reviewed every call site of
r.subscriptionsin the file after the change to confirm all reads and writes are now consistently lock-protected, and that no lock is held during any blocking remote call (sub.RootChainInfo,sub.ValidatorSet,sub.Orders,sub.Order,sub.IsValidDoubleSigner,sub.MinimumEvidenceHeight,sub.Checkpoint,sub.Lottery,sub.Transaction,sub.DexBatch) to avoid introducing new lock-contention or deadlock risk.