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

Issue 1606: Fixed race condition during expansion of concurrent open hash maps #1607

Closed
wants to merge 1 commit into from

Conversation

merlimat
Copy link
Contributor

Motivation

As reported in #1606, there is a race condition in the concurrent open hash maps implementation. The race happens when the maps gets re-hashed after the expansion and the new arrays are substituting the old ones.

The race itself is that a thread doing a get() on the map is first checking the current capacity of the map, uses that to get the bucket and then tries to do optimistic read of the value in that bucket.

This assumes capacity update is visible only after the values array is already swapped, but that is not always the case in current code.

Changes

  • Use volatile qualifier for capacity and values arrays to ensure ordering of memory read is respected by compiler
  • In rehashing, update capacity after values

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

Makes sense.
Good catch

+1

@sijie
Copy link
Member

sijie commented Aug 16, 2018

run pr validation

@merlimat merlimat added this to the 4.8.0 milestone Aug 16, 2018
@merlimat merlimat closed this in a7e66e1 Aug 16, 2018
merlimat added a commit that referenced this pull request Aug 16, 2018
… hash maps

### Motivation

As reported in #1606, there is a race condition in the concurrent open hash maps implementation. The race happens when the maps gets re-hashed after the expansion and the new arrays are substituting the old ones.

The race itself is that a thread doing a `get()` on the map is first checking the current `capacity` of the map, uses that to get the bucket and then tries to do optimistic read of the value in that bucket.

This assumes `capacity` update is visible only after the `values` array is already swapped, but that is not always the case in current code.

### Changes
 * Use `volatile` qualifier for `capacity` and `values` arrays to ensure ordering of memory read is respected by compiler
 * In rehashing, update `capacity` after `values`

Author: Matteo Merli <mmerli@apache.org>

Reviewers: Enrico Olivelli <eolivelli@gmail.com>, Sijie Guo <sijie@apache.org>

This closes #1607 from merlimat/fix-concurrent-maps, closes #1606

(cherry picked from commit a7e66e1)
Signed-off-by: Matteo Merli <mmerli@apache.org>
merlimat added a commit that referenced this pull request Aug 16, 2018
… hash maps

### Motivation

As reported in #1606, there is a race condition in the concurrent open hash maps implementation. The race happens when the maps gets re-hashed after the expansion and the new arrays are substituting the old ones.

The race itself is that a thread doing a `get()` on the map is first checking the current `capacity` of the map, uses that to get the bucket and then tries to do optimistic read of the value in that bucket.

This assumes `capacity` update is visible only after the `values` array is already swapped, but that is not always the case in current code.

### Changes
 * Use `volatile` qualifier for `capacity` and `values` arrays to ensure ordering of memory read is respected by compiler
 * In rehashing, update `capacity` after `values`

Author: Matteo Merli <mmerli@apache.org>

Reviewers: Enrico Olivelli <eolivelli@gmail.com>, Sijie Guo <sijie@apache.org>

This closes #1607 from merlimat/fix-concurrent-maps, closes #1606

(cherry picked from commit a7e66e1)
Signed-off-by: Matteo Merli <mmerli@apache.org>
@merlimat
Copy link
Contributor Author

Merged in master and ported to branch-4.8 (07bb42a) and branch-4.7 (36edaca)

@merlimat merlimat deleted the fix-concurrent-maps branch August 16, 2018 16:56
sijie pushed a commit to apache/pulsar that referenced this pull request Aug 17, 2018
…2387)

### Motivation

Porting same fix as apache/bookkeeper#1607 to correct issue reported on apache/bookkeeper#1606.

There is a race condition in the concurrent open hash maps implementation. The race happens when the maps gets re-hashed after the expansion and the new arrays are substituting the old ones.

The race itself is that a thread doing a `get()` on the map is first checking the current capacity of the map, uses that to get the bucket and then tries to do optimistic read of the value in that bucket.

This assumes `capacity` update is visible only after the `values` array is already swapped, but that is not always the case in current code.

### Changes

 * Use `volatile` qualifier for `capacity` and `values` arrays to ensure ordering of memory read is respected by compiler
 * In rehashing, update `capacity` after `values` where it was not already the case
sijie pushed a commit to apache/pulsar that referenced this pull request Aug 27, 2018
…2387)

### Motivation

Porting same fix as apache/bookkeeper#1607 to correct issue reported on apache/bookkeeper#1606.

There is a race condition in the concurrent open hash maps implementation. The race happens when the maps gets re-hashed after the expansion and the new arrays are substituting the old ones.

The race itself is that a thread doing a `get()` on the map is first checking the current capacity of the map, uses that to get the bucket and then tries to do optimistic read of the value in that bucket.

This assumes `capacity` update is visible only after the `values` array is already swapped, but that is not always the case in current code.

### Changes

 * Use `volatile` qualifier for `capacity` and `values` arrays to ensure ordering of memory read is respected by compiler
 * In rehashing, update `capacity` after `values` where it was not already the case
reddycharan pushed a commit to reddycharan/bookkeeper that referenced this pull request Oct 17, 2018
…t open hash maps

### Motivation

As reported in apache#1606, there is a race condition in the concurrent open hash maps implementation. The race happens when the maps gets re-hashed after the expansion and the new arrays are substituting the old ones.

The race itself is that a thread doing a `get()` on the map is first checking the current `capacity` of the map, uses that to get the bucket and then tries to do optimistic read of the value in that bucket.

This assumes `capacity` update is visible only after the `values` array is already swapped, but that is not always the case in current code.

### Changes
 * Use `volatile` qualifier for `capacity` and `values` arrays to ensure ordering of memory read is respected by compiler
 * In rehashing, update `capacity` after `values`

Author: Matteo Merli <mmerli@apache.org>

Reviewers: Enrico Olivelli <eolivelli@gmail.com>, Sijie Guo <sijie@apache.org>

This closes apache#1607 from merlimat/fix-concurrent-maps, closes apache#1606

(cherry picked from commit a7e66e1)
Signed-off-by: Matteo Merli <mmerli@apache.org>
(cherry picked from commit 36edaca)
Signed-off-by: JV Jujjuri <vjujjuri@salesforce.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants