From 2781096b578816fbf4d7c00423142e63d0ae1525 Mon Sep 17 00:00:00 2001 From: tycho garen Date: Fri, 11 Mar 2022 12:17:53 -0500 Subject: [PATCH] elide deadlock --- internal/consensus/state.go | 51 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/internal/consensus/state.go b/internal/consensus/state.go index 77ba1780e..29b2b338d 100644 --- a/internal/consensus/state.go +++ b/internal/consensus/state.go @@ -317,28 +317,41 @@ func (cs *State) GetValidators() (int64, []*types.Validator) { // SetPrivValidator sets the private validator account for signing votes. It // immediately requests pubkey and caches it. func (cs *State) SetPrivValidator(ctx context.Context, priv types.PrivValidator) { - cs.mtx.Lock() - defer cs.mtx.Unlock() + func() { + cs.mtx.Lock() + defer cs.mtx.Unlock() - cs.privValidator = priv + cs.privValidator = priv + }() if priv != nil { - switch t := priv.(type) { - case *privval.RetrySignerClient: - cs.privValidatorType = types.RetrySignerClient - case *privval.FilePV: - cs.privValidatorType = types.FileSignerClient - case *privval.SignerClient: - cs.privValidatorType = types.SignerSocketClient - case *tmgrpc.SignerClient: - cs.privValidatorType = types.SignerGRPCClient - case types.MockPV: - cs.privValidatorType = types.MockSignerClient - case *types.ErroringMockPV: - cs.privValidatorType = types.ErrorMockSignerClient - default: - cs.logger.Error("unsupported priv validator type", "err", - fmt.Errorf("error privValidatorType %s", t)) + ptype, err := func() (types.PrivValidatorType, error) { + switch t := priv.(type) { + case *privval.RetrySignerClient: + return types.RetrySignerClient, nil + case *privval.FilePV: + return types.FileSignerClient, nil + case *privval.SignerClient: + return types.SignerSocketClient, nil + case *tmgrpc.SignerClient: + return types.SignerGRPCClient, nil + case types.MockPV: + return types.MockSignerClient, nil + case *types.ErroringMockPV: + return types.ErrorMockSignerClient, nil + default: + return 0, fmt.Errorf("error privValidatorType %s", t) + } + }() + if err != nil { + cs.logger.Error("unsupported priv validator type", "err", err) + } else { + func() { + cs.mtx.Lock() + defer cs.mtx.Unlock() + + cs.privValidatorType = ptype + }() } }