Skip to content
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

Remove only works with the last peer of the list #14

Closed
ernestoalejo opened this issue Mar 25, 2020 · 0 comments · Fixed by #17
Closed

Remove only works with the last peer of the list #14

ernestoalejo opened this issue Mar 25, 2020 · 0 comments · Fixed by #17

Comments

@ernestoalejo
Copy link

This code captures only the last iteration of the for loop. When the goroutines finally run the variable peer has already changed to the last value.

		// Asynchronously clear the key from all hot and main caches of peers
		for _, peer := range g.peers.GetAll() {
			// avoid deleting from owner a second time
			if peer == owner {
				continue
			}

			wg.Add(1)
			go func() {
				errs <- g.removeFromPeer(ctx, peer, key)
				wg.Done()
			}()
		}

You can see the same efect here: https://play.golang.org/p/I82KNfRkqSP
Go vet will also highlight the error:

./prog.go:16:19: loop variable peer captured by func literal

The solution here is easy though. The smallest working change is to pass the peer as an argument to the goroutine: https://play.golang.org/p/0mNV3nSkGUy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant