allora-network / allora-chain

Node software to run the Allora Network
https://www.allora.network/
Apache License 2.0
73 stars 67 forks source link

Prevent potential race condition by making parallelized pass-by-copy rather than reference #442

Closed relyt29 closed 1 month ago

relyt29 commented 1 month ago

What is the purpose of the change

This PR closes a subtle race condition bug that could potentially happen anywhere that for-loops are spawned with go func - the for loop operates on a reference which changes faster than the function call can potentially happen, causing a function to use the wrong parameter when referenced.

The solution is very simple, make a local copy of the variable and pass the resulting function by copy, rather than by reference - then there's no way the for loop can accidentally reference the wrong data.

Testing and Verifying

This change is hard to test, so we're not going to write a unit test to simulate it explicitly. The performance implicaitons of doing a pass-by-copy are trivial, therefore there is no reason to not do the safer thing. Our existing test suite should test that the change does not have correctness implications.

Documentation and Release Note

This PR has no implications for documentation.