-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(bug): use copy of loop variable in Go routines #8163
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@skrdgraph I recommend that this get merged, good catch by @renatolabs here
@renatolabs thanks for the PR. 👍 to merge this (it does make sense to get this in asap) I guess we also have similar issues in our |
@renatolabs - could you rebase with latest changes and push again please? We would like to get this in once the tests pass. The rebase should pull in our latest test CI setup. |
This fixes cases where loop variables are incorrectly captured in asynchronous code (parallel tests or Go routines). In the case of a parallel test, the most likely scenario is that only the last test case will be executed. In the case of Go routines that directly access loop variables without any synchronization, the value stored in the loop variable will likely not be the same as what it was when the Go routine is created.
Done! |
Thanks, I have approved a CI run to happen on this PR now. Ill merge it one once it completes. |
@kevinmingtarja we need to look at why the COVERALLS token was not passed. The tests did pass - I am going to merge this, and if this continues to be an issue, we should look into this. |
For future reference I want to add a link to a reference here on Go.dev that mentions this issue. It seems like a common point of confusion in Go, and this PR is indeed idiomatic Go. One could equivalently pass the variables as arguments to the closure but this also works. |
This fixes cases where loop variables are incorrectly captured in
asynchronous code (parallel tests or Go routines). In the case of a
parallel test, the most likely scenario is that only the last test
case will be executed. In the case of Go routines that directly
access loop variables without any synchronization, the value stored in
the loop variable will likely not be the same as what it was when the
Go routine is created.