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

Bool is not atomic #59

Merged
merged 1 commit into from
Jul 25, 2017
Merged

Bool is not atomic #59

merged 1 commit into from
Jul 25, 2017

Conversation

timou
Copy link
Contributor

@timou timou commented Jul 19, 2017

The Go race detector doesn't like concurrent access to bool. This change
secures the bool with a mutex.

@msftclas
Copy link

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot

@darstahl
Copy link
Contributor

darstahl commented Jul 24, 2017

We already have an "atomicBool" type in this file used for a different bool with a similar issue. I'd prefer to use that instead of adding a Mutex.

type atomicBool int32

func (b *atomicBool) isSet() bool { return atomic.LoadInt32((*int32)(b)) != 0 }
func (b *atomicBool) setFalse()   { atomic.StoreInt32((*int32)(b), 0) }
func (b *atomicBool) setTrue()    { atomic.StoreInt32((*int32)(b), 1) }

See deadlineHandler.timedout for example usage.

@timou
Copy link
Contributor Author

timou commented Jul 24, 2017

That's fine, and I'm happy to change that, but I also think there is a genuine race between inspecting the bool and setting it (lines 112-114 of file.go). I don't think that can be solved by atomicbool.

@timou
Copy link
Contributor Author

timou commented Jul 24, 2017

OK, I lie. I have extended atomicBool with compareAndSwap, which I think fixes the race and avoids the mutex.

file.go Outdated
@@ -107,9 +117,9 @@ func MakeOpenFile(h syscall.Handle) (io.ReadWriteCloser, error) {

// closeHandle closes the resources associated with a Win32 handle
func (f *win32File) closeHandle() {
if !f.closing {
swapped := f.closing.compareAndSwap(false, true)
if swapped {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be all one line.

@darstahl
Copy link
Contributor

darstahl commented Jul 24, 2017

@timou I was about to comment almost the same fix.

While compareAndSwap works, as per an offline conversation with @jstarks only a swap is needed here (and is faster).

func (b *atomicBool) swap(new bool) (old bool) {
	var newInt int32
	if new {
		newInt = 1
	}
	return atomic.SwapInt32((*int32)(b), newInt) == 1
}

@darstahl
Copy link
Contributor

Could you also squash your commits into a single commit before we merge this?

Copy link
Contributor

@darstahl darstahl left a comment

Choose a reason for hiding this comment

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

LGTM

@darstahl
Copy link
Contributor

There's still a second (blank) merge commit. I'm good to merge if you can remove that.

The Go race detector doesn't like concurrent access to bool. This change
moves the bool to use atomicBool, and extends atomicBool with the swap()
method, to handle conditional use.
@timou
Copy link
Contributor Author

timou commented Jul 25, 2017

OK, rebased. Thanks for the nice review!

@darstahl darstahl merged commit 7ff8994 into microsoft:master Jul 25, 2017
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.

3 participants