From 78ac72c7e0661128ae29b88900009a59386cae29 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Sun, 28 Jan 2024 07:06:23 -0800 Subject: [PATCH] Fix P-chain validator set lookup race condition (#2673) Co-authored-by: Stephen Buttolph --- vms/platformvm/vm_regression_test.go | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/vms/platformvm/vm_regression_test.go b/vms/platformvm/vm_regression_test.go index 0a87758fdb1b..f4e84d0776ae 100644 --- a/vms/platformvm/vm_regression_test.go +++ b/vms/platformvm/vm_regression_test.go @@ -2245,21 +2245,31 @@ func TestValidatorSetRaceCondition(t *testing.T) { protocolAppRequestBytest, ) - var eg errgroup.Group - for i := 0; i < 100; i++ { + var ( + eg errgroup.Group + ctx, cancel = context.WithCancel(context.Background()) + ) + // keep 10 workers running + for i := 0; i < 10; i++ { eg.Go(func() error { - return vm.AppRequest( - context.Background(), - nodeID, - 0, - time.Now().Add(time.Hour), - appRequestBytes, - ) + for ctx.Err() == nil { + err := vm.AppRequest( + context.Background(), + nodeID, + 0, + time.Now().Add(time.Hour), + appRequestBytes, + ) + if err != nil { + return err + } + } + return nil }) } // If the validator set lock isn't held, the race detector should fail here. - for i := uint64(0); i < 100; i++ { + for i := uint64(0); i < 1000; i++ { blk, err := block.NewBanffStandardBlock( time.Now(), vm.state.GetLastAccepted(), @@ -2276,6 +2286,7 @@ func TestValidatorSetRaceCondition(t *testing.T) { // If the validator set lock is grabbed, we need to make sure to release the // lock to avoid a deadlock. vm.ctx.Lock.Unlock() + cancel() // stop and wait for workers require.NoError(eg.Wait()) vm.ctx.Lock.Lock() }