-
Notifications
You must be signed in to change notification settings - Fork 272
fix: guard capabilities map with RWMutex #3092
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
Open
rachit77
wants to merge
2
commits into
berachain:main
Choose a base branch
from
rachit77:fix/capabilities-map-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| // | ||
| // Copyright (C) 2025, Berachain Foundation. All rights reserved. | ||
| // Use of this software is governed by the Business Source License included | ||
| // in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
| // | ||
| // ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
| // TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
| // VERSIONS OF THE LICENSED WORK. | ||
| // | ||
| // THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
| // LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
| // LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
| // | ||
| // TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
| // AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
| // EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
| // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
| // TITLE. | ||
|
|
||
| package client | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestCapabilitiesRace verifies that concurrent writes (ExchangeCapabilities) | ||
| // and reads (HasCapability) on the capabilities map do not race. | ||
| func TestCapabilitiesRace(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| c := &EngineClient{ | ||
| capabilities: make(map[string]struct{}), | ||
| } | ||
|
|
||
| const cap = "engine_newPayloadV3" | ||
| const iterations = 1000 | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(2) | ||
|
|
||
| // Goroutine 1: repeatedly write a capability (simulates ExchangeCapabilities). | ||
| go func() { | ||
| defer wg.Done() | ||
| for range iterations { | ||
| c.capabilitiesMu.Lock() | ||
| c.capabilities[cap] = struct{}{} | ||
| c.capabilitiesMu.Unlock() | ||
| } | ||
| }() | ||
|
|
||
| // Goroutine 2: repeatedly read a capability (simulates HasCapability). | ||
| go func() { | ||
| defer wg.Done() | ||
| for range iterations { | ||
| c.HasCapability(cap) | ||
| } | ||
| }() | ||
|
|
||
| wg.Wait() | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for range iterationsdoes not compile becauseiterationsis an untyped integer constant, not an iterable. Use a counted loop (e.g., incrementing index) or range over a slice of lengthiterationsso the test builds and can be exercised undergo test -race.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for range iterations is valid
Go 1.22+syntax for ranging over an integer constant, and this repo targets Go1.26, so this compiles and runs correctly