[race bug-fix] - fix reader-writer race check in metrics middleware #39
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.
description
usually, we create a runnerChain once in a service and execute the same runnerChain by providing different execution logics.
they mostly runs parallely which can leads to race condition in metrics middleware.
current code: (with race condition, detected in go -race also)
the metrics/runner.go -> newMiddleware() function executes parallely.
in this, the two lines, given below, sharing the next variable in all parallel calls that can leads to classic reader-writer problem over next variable resource causing go -race check fails.
next = goresilience.SanitizeRunner(next)
-> write at nexterr = next.Run(ctx, f)
-> read at nextsolution approach
I realise that we don't need to read and write over next variable in every metrics layer's runner call. instead, we can write once at next during assignment of
next = goresilience.SanitizeRunner(next)
variable outside the returned runnerFunc and read line at next variable can be there as it is. so, there will be only read operations exists during parallel execution. hence, no need to sync the read and write over next variable.to verify race condition