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

client: stop the background thread before closing #423

Merged
merged 2 commits into from
Apr 15, 2015

Conversation

eapache
Copy link
Contributor

@eapache eapache commented Apr 15, 2015

Otherwise, in the rare case where a background metadata refresh coincided
exactly with a call to Client.Close() there was a race where we might have
potentially tried to write to a nil map. Also add a test for this.

Fixes #422.

@Shopify/kafka @epsniff

@@ -151,6 +152,10 @@ func (client *client) Close() error {
return ErrClosedClient
}

// shutdown and wait for the background thread before we take the lock, to avoid races
close(client.closer)
<-client.closed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The job still gets started, it just shuts down immediately (see lines 536-540). The close(client.closed) call is deferred, so if it returns immediately the channel still gets closed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just saw that, thanks! deleting my comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will, however, deadlock if the client calls Close() on itself because of an error during initialization. So something still to fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

eapache added 2 commits April 15, 2015 19:11
Otherwise, in the rare case where a background metadata refresh coincided
exactly with a call to `Client.Close()` there was a race where we might have
potentially tried to write to a nil map. Also add a test for this.

Fixes #422.
To catch if anybody accidentally breaks client startup/shutdown in this
configuration.
@eapache eapache force-pushed the client-shutdown-race branch from 9c715b9 to 52af5d9 Compare April 15, 2015 19:11
@wvanbergen
Copy link
Contributor

Approach looks good. Too bad the test needs time.Sleep like that. :/

@eapache
Copy link
Contributor Author

eapache commented Apr 15, 2015

I'm not as concerned about this test, since the sleeps are mainly to accommodate the timer of the background thread (whose value we control, but which can't just be 0) rather than to ensure things happen in a very particular order. I don't expect this test to be flaky with respect to future changes.

eapache added a commit that referenced this pull request Apr 15, 2015
client: stop the background thread before closing
@eapache eapache merged commit 8f3630c into master Apr 15, 2015
@eapache eapache deleted the client-shutdown-race branch April 15, 2015 20:27
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.

Panic: assignment to entry in nil map
3 participants