这是indexloc提供的服务,不要输入任何密码
Skip to content

Ensure maps are accessed with a lock #1434

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

Merged
merged 1 commit into from
Jun 23, 2022
Merged

Conversation

gsoltis
Copy link
Contributor

@gsoltis gsoltis commented Jun 23, 2022

Also ensure mutex is captured by reference, rather than by value.

Fixes #1433

@vercel
Copy link

vercel bot commented Jun 23, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
turbo-site ⬜️ Ignored (Inspect) Jun 23, 2022 at 10:41PM (UTC)

@gsoltis gsoltis marked this pull request as ready for review June 23, 2022 22:42
@kodiakhq kodiakhq bot merged commit fc0abe7 into main Jun 23, 2022
@kodiakhq kodiakhq bot deleted the gsoltis/lock_map_access branch June 23, 2022 22:51
Copy link
Contributor

@nathanhammond nathanhammond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning this up! 😅

I have questions about why this failed that I'm going to spend some time learning today.

@@ -308,13 +308,15 @@ func getTraversePath(rootPath turbopath.AbsoluteSystemPath) (turbopath.RelativeU
// Don't shell out if we already know where you are in the repository.
// `memoize` is a good candidate for generics.
func memoizeGetTraversePath() func(turbopath.AbsoluteSystemPath) (turbopath.RelativeUnixPath, error) {
cacheMutex := sync.RWMutex{}
cacheMutex := &sync.RWMutex{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's caught in a closure I didn't believe it was necessary to indirect it? Maybe my mental model for how closure scoped vars get addressed in Go is wrong?


type RWMutex

[func (rw *RWMutex) Lock()](https://pkg.go.dev/sync#RWMutex.Lock)
[func (rw *RWMutex) RLock()](https://pkg.go.dev/sync#RWMutex.RLock)
[func (rw *RWMutex) RLocker() Locker](https://pkg.go.dev/sync#RWMutex.RLocker)
[func (rw *RWMutex) RUnlock()](https://pkg.go.dev/sync#RWMutex.RUnlock)
[func (rw *RWMutex) TryLock() bool](https://pkg.go.dev/sync#RWMutex.TryLock)
[func (rw *RWMutex) TryRLock() bool](https://pkg.go.dev/sync#RWMutex.TryRLock)
[func (rw *RWMutex) Unlock()](https://pkg.go.dev/sync#RWMutex.Unlock)

Given these method signatures, why did Go let me do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second readthrough, I think you're right. The fact that it's in a closure and not an argument means it's captured by reference, and the style guide at least suggests that we ought to change it to var cacheMutex sync.RWMutex. The culprit was likely not locking around the reads from the maps.

For the methods on RWMutex, unfortunately, I believe Go will let you call them with or without a reference, without having an indication at the call site. The implementation is choosing whether the receiver is passed by reference or by value.

If you haven't already seen it, I recommend reading Uber's analysis of data races in Go. I think I confused the interaction between 1. and 4. in that article, but got 3. correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that the zero value of a mutex is a valid mutex.

The thing that threw me here was not knowing I needed a reader lock; I (incorrectly) assumed that locks during writes would be enough, but realize now that it doesn't either implicitly lock out readers during the write lock and the corollary implication that writes aren't necessarily atomic.

Arguably this should be using sync.Map but that loses types and becomes a pain to use.

The Uber data races post was in a browser tab yesterday as to-read, and, well, now it has been read. About 12 hours too late.

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 this pull request may close these issues.

Exception thrown after upgrading to 1.3.0
3 participants