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

Fix data race b/c of lacking lock protection for "data" slice #6017

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions ci/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func TestWithCoverage() error {
return err
}
args := append([]string{"test", "-race", "-timeout", "5m", "-cover", "-coverprofile", "coverage.txt", "-covermode", "atomic"}, packages...)
return sh.RunV("go", args...)

env := make(map[string]string)
env["GORACE"] = "halt_on_error=1"
return sh.RunWith(env, "go", args...)
}

// Test runs unit tests
Expand All @@ -39,8 +42,11 @@ func Test() error {
if err != nil {
return err
}
args := append([]string{"test", "-race", "-count=1", "-timeout", "5m"}, packages...)
return sh.RunV("go", args...)
args := append([]string{"test", "-race", "-timeout", "5m"}, packages...)

env := make(map[string]string)
env["GORACE"] = "halt_on_error=1"
return sh.RunWith(env, "go", args...)
}

func unitTestPackages() ([]string, error) {
Expand Down
55 changes: 29 additions & 26 deletions session/pingpong/consumer_balance_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,34 +364,37 @@ func TestConsumerBalanceTracker_InprogressUnregisteredBalanceReturnedWhenNoBount
}

func TestConsumerBalanceTracker_RecoverGrandTotalPromisedSettledIsBiggerThanPromissedNotOffChain(t *testing.T) {
id1 := identity.FromAddress("0x000000001")
grandTotalPromised := big.NewInt(10)
settledAmount := big.NewInt(11)
bus := eventbus.New()
mcts := NewConsumerTotalsStorage(bus)
bc := mockConsumerBalanceChecker{}
cfg := defaultCfg
cfg.LongSync.Interval = time.Millisecond * 300
calc := newMockAddressProvider()
calc.addrToReturn = id1.ToCommonAddress()
mockBlockchainProvider := &mockBlockchainInfoProvider{}

mockBlockchainProvider.AddConsumerChannelsHermes(1, id1.ToCommonAddress(), client.ConsumersHermes{
Settled: big.NewInt(6),
})
// make data race more likely to happen
for i := 0; i < 10; i++ {
id1 := identity.FromAddress("0x000000001")
grandTotalPromised := big.NewInt(10)
settledAmount := big.NewInt(11)
bus := eventbus.New()
mcts := NewConsumerTotalsStorage(bus)
bc := mockConsumerBalanceChecker{}
cfg := defaultCfg
cfg.LongSync.Interval = time.Millisecond * 300
calc := newMockAddressProvider()
calc.addrToReturn = id1.ToCommonAddress()
mockBlockchainProvider := &mockBlockchainInfoProvider{}

mockBlockchainProvider.AddConsumerChannelsHermes(1, id1.ToCommonAddress(), client.ConsumersHermes{
Settled: big.NewInt(6),
})

cbt := NewConsumerBalanceTracker(bus, &bc, mcts, &mockconsumerInfoGetter{grandTotalPromised, settledAmount}, &mockTransactor{}, &mockRegistrationStatusProvider{}, calc, mockBlockchainProvider, defaultCfg)
cbt := NewConsumerBalanceTracker(bus, &bc, mcts, &mockconsumerInfoGetter{grandTotalPromised, settledAmount}, &mockTransactor{}, &mockRegistrationStatusProvider{}, calc, mockBlockchainProvider, defaultCfg)

err := cbt.Subscribe(bus)
assert.NoError(t, err)
bus.Publish(identity.AppTopicIdentityUnlock, identity.AppEventIdentityUnlock{
ChainID: 1,
ID: id1,
})
assert.Eventually(t, func() bool {
savedBalance, _ := mcts.Get(1, id1, common.BigToAddress(big.NewInt(0)))
return savedBalance.Cmp(big.NewInt(6)) == 0
}, defaultWaitTime, defaultWaitInterval)
err := cbt.Subscribe(bus)
assert.NoError(t, err)
bus.Publish(identity.AppTopicIdentityUnlock, identity.AppEventIdentityUnlock{
ChainID: 1,
ID: id1,
})
assert.Eventually(t, func() bool {
savedBalance, _ := mcts.Get(1, id1, common.BigToAddress(big.NewInt(0)))
return savedBalance.Cmp(big.NewInt(6)) == 0
}, defaultWaitTime, defaultWaitInterval)
}
}

type mockConsumerBalanceChecker struct {
Expand Down
3 changes: 3 additions & 0 deletions session/pingpong/consumer_totals_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func (cts *ConsumerTotalsStorage) Store(chainID int64, id identity.Identity, her
// Get fetches the amount as promised for the given channel.
func (cts *ConsumerTotalsStorage) Get(chainID int64, id identity.Identity, hermesID common.Address) (*big.Int, error) {
key := cts.makeKey(chainID, id, hermesID)
cts.createLock.Lock()
defer cts.createLock.Unlock()

element, ok := cts.data[key]
if !ok {
return nil, ErrNotFound
Expand Down