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

producer: fix connection race #133

Merged
merged 1 commit into from
Apr 8, 2015
Merged

producer: fix connection race #133

merged 1 commit into from
Apr 8, 2015

Conversation

jeddenlea
Copy link
Contributor

Prior to this change, it is possible for more than one goroutine to
check:

if atomic.LoadInt32(&w.state) != StateConnected {

...thereafter calling w.connect. w.connect first grabs a lock on
w.guard, and then asserts that it be the only goroutine attempting a
connection:

if !atomic.CompareAndSwapInt32(&w.state, StateInit, StateConnected) {
    return ErrNotConnected
}

This means if there are two parallel actions which attempt to initiate
the connection, one of them will return ErrNotConnected if the first
succesfully connected.

Prior to this change, it is possible for more than one goroutine to
check:

	if atomic.LoadInt32(&w.state) != StateConnected {

...thereafter calling w.connect.  w.connect first grabs a lock on
w.guard, and then asserts that it be the only goroutine attempting a
connection:

	if !atomic.CompareAndSwapInt32(&w.state, StateInit, StateConnected) {
		return ErrNotConnected
	}

This means if there are two parallel actions which attempt to initiate
the connection, one of them will return ErrNotConnected if the first
succesfully connected.
@mreiferson
Copy link
Member

LGTM, nice catch!

Thanks!

mreiferson added a commit that referenced this pull request Apr 8, 2015
producer: fix connection race
@mreiferson mreiferson merged commit a40c61c into nsqio:master Apr 8, 2015
@mreiferson mreiferson added the bug label Apr 8, 2015
mreiferson added a commit to mreiferson/go-nsq that referenced this pull request Apr 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants