Solving race condition when context times out. #100
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.
This solves issue #99 . (used chatGPT to help me explain the pull request)
Why Goroutines Kept Running After Timeout
When the context times out, the main routine continues execution, but the goroutine keeps running in the background.
Each new iteration spawns another goroutine, so multiple background goroutines end up modifying the shared
pubkeyFollowerCountmap at the same time.This leads to a race condition that manifests later in
updateWoTMap().Problem Explanation
Step by step:
timeoutCtxexpires first, theselectunblocks and the main routine continues to the next iteration.pubkeyFollowerCount.updateWoTMap()runs, previous goroutines may still be writing → data race.Fix
Replace the goroutine with an immediately invoked synchronous function:
Why Running Synchronously Is Safe
Running the function synchronously does not harm performance or behavior because:
donechannel before proceeding, so the code effectively behaved synchronously already — just with unnecessary concurrency and potential races.FetchMany, which respectstimeoutCtx. If the timeout triggers, the function returns promptly.FetchManyitself, so executing it inline adds negligible latency.In short, removing the goroutine eliminates background writes without changing timing or responsiveness — the main routine still respects the timeout, and execution remains efficient and deterministic.