Skip to content

Commit

Permalink
fix: protect request params against race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Sep 6, 2024
1 parent a2370d2 commit 1027da6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
15 changes: 11 additions & 4 deletions common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,22 @@ func (r *NormalizedRequest) Body() []byte {

func (r *NormalizedRequest) MarshalZerologObject(e *zerolog.Event) {
if r != nil {
if r.body != nil {
e.Str("request", string(r.body))
} else if r.jsonRpcRequest != nil {
e.Object("request", r.jsonRpcRequest)
if r.jsonRpcRequest != nil {
e.Object("jsonRpc", r.jsonRpcRequest)
} else if r.body != nil {
e.Str("body", string(r.body))
}
}
}

func (r *NormalizedRequest) EvmBlockNumber() (int64, error) {
if r == nil {
return 0, nil
}

r.Mu.RLock()
defer r.Mu.RUnlock()

rpcReq, err := r.JsonRpcRequest()
if err != nil {
return 0, err
Expand Down
4 changes: 4 additions & 0 deletions erpc/evm_json_rpc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (c *EvmJsonRpcCache) Get(ctx context.Context, req *common.NormalizedRequest
return nil, err
}

req.Mu.RLock()
blockRef, blockNumber, err := common.ExtractEvmBlockReferenceFromRequest(rpcReq)
req.Mu.RUnlock()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -120,7 +122,9 @@ func (c *EvmJsonRpcCache) Set(ctx context.Context, req *common.NormalizedRequest
return err
}

req.Mu.RLock()
blockRef, blockNumber, err := common.ExtractEvmBlockReference(rpcReq, rpcResp)
req.Mu.RUnlock()
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions erpc/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func setResponseStatusCode(respOrErr interface{}, fastCtx *fasthttp.RequestCtx)

func processErrorBody(logger *zerolog.Logger, nq *common.NormalizedRequest, err error) interface{} {
if !common.IsNull(err) {
if nq != nil {
nq.Mu.RLock()
}
if common.HasErrorCode(err, common.ErrCodeEndpointClientSideException) {
logger.Debug().Object("request", nq).Err(err).Msgf("forward request errored with client-side exception")
} else {
Expand All @@ -394,6 +397,9 @@ func processErrorBody(logger *zerolog.Logger, nq *common.NormalizedRequest, err
logger.Error().Object("request", nq).Err(err).Msgf("failed to forward request: %s", err.Error())
}
}
if nq != nil {
nq.Mu.RUnlock()
}
}

// TODO extend this section to detect transport mode (besides json-rpc) when more modes are added.
Expand Down
12 changes: 5 additions & 7 deletions upstream/failsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,22 @@ func createCircuitBreakerPolicy(logger *zerolog.Logger, component string, cfg *c
builder.OnFailure(func(event failsafe.ExecutionEvent[*common.NormalizedResponse]) {
err := event.LastError()
res := event.LastResult()
lg := logger.Warn().Err(err).Interface("response", res)
if res != nil && !res.IsObjectNull() {
rq := res.Request()
if rq != nil {
lg = lg.Object("request", rq)
up := rq.LastUpstream()
if up != nil {
lg = lg.Str("upstreamId", up.Config().Id)
cfg := up.Config()
if cfg.Evm != nil {
logger.Warn().Err(err).Interface("lastResult", res).Interface("upstreamSyncing", cfg.Evm.Syncing).Msgf("failure caught that will be considered for circuit breaker")
return
} else {
logger.Warn().Err(err).Interface("lastResult", res).Msgf("failure caught that will be considered for circuit breaker")
return
lg = lg.Interface("upstreamSyncingState", cfg.Evm.Syncing)
}
}
}
}

logger.Warn().Err(err).Msgf("failure caught that will be considered for circuit breaker")
lg.Msg("failure caught that will be considered for circuit breaker")
})

builder.HandleIf(func(result *common.NormalizedResponse, err error) bool {
Expand Down

0 comments on commit 1027da6

Please sign in to comment.