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

race issue in runContainer16's equal() method #305

Closed
beiwei30 opened this issue May 27, 2021 · 3 comments
Closed

race issue in runContainer16's equal() method #305

beiwei30 opened this issue May 27, 2021 · 3 comments

Comments

@beiwei30
Copy link

Run the following code with go test -race:

package roaring

import (
	"github.com/RoaringBitmap/roaring"
	"sync"
	"testing"
)

func TestJoinIfNotEqual(t *testing.T) {
	l := roaring.NewBitmap()
	l.Add(uint32(1))
	l.Add(uint32(2))
	l.Add(uint32(3))
	r := roaring.NewBitmap()
	r.AddRange(1, 4)

	// this is for race condition test
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		l.Equals(r)
	}()

	go func() {
		defer wg.Done()
		l.Equals(r)
	}()
	wg.Wait()
}

Race detector will report the following warning to indicate there's potential data race in runContainer16's equal() method:

=== RUN   TestEqual
==================
WARNING: DATA RACE
Read at 0x00c00015e058 by goroutine 9:
  github.com/RoaringBitmap/roaring.(*runContainer16).cardinality()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/runcontainer.go:984 +0x64
  github.com/RoaringBitmap/roaring.(*runContainer16).getCardinality()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/runcontainer.go:2337 +0x27c
  github.com/RoaringBitmap/roaring.(*runContainer16).equals()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/runcontainer.go:2108 +0x246
  github.com/RoaringBitmap/roaring.(*roaringArray).equals()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/roaringarray.go:434 +0x2ba
  github.com/RoaringBitmap/roaring.(*Bitmap).Equals()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/roaring.go:495 +0xe4
  test-go/roaring.TestEqual.func2()
      /Users/iluo/tmp/test-go/roaring/roaring_map_test.go:27 +0x86

Previous write at 0x00c00015e058 by goroutine 8:
  github.com/RoaringBitmap/roaring.(*runContainer16).cardinality()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/runcontainer.go:992 +0xfc
  github.com/RoaringBitmap/roaring.(*runContainer16).getCardinality()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/runcontainer.go:2337 +0x27c
  github.com/RoaringBitmap/roaring.(*runContainer16).equals()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/runcontainer.go:2108 +0x246
  github.com/RoaringBitmap/roaring.(*roaringArray).equals()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/roaringarray.go:434 +0x2ba
  github.com/RoaringBitmap/roaring.(*Bitmap).Equals()
      /Users/iluo/go/pkg/mod/github.com/!roaring!bitmap/roaring@v0.6.1/roaring.go:495 +0xe4
  test-go/roaring.TestEqual.func1()
      /Users/iluo/tmp/test-go/roaring/roaring_map_test.go:22 +0x86

Goroutine 9 (running) created at:
  test-go/roaring.TestEqual()
      /Users/iluo/tmp/test-go/roaring/roaring_map_test.go:25 +0x21a
  testing.tRunner()
      /usr/local/Cellar/go/1.16.3/libexec/src/testing/testing.go:1193 +0x202

Goroutine 8 (finished) created at:
  test-go/roaring.TestEqual()
      /Users/iluo/tmp/test-go/roaring/roaring_map_test.go:20 +0x1e4
  testing.tRunner()
      /usr/local/Cellar/go/1.16.3/libexec/src/testing/testing.go:1193 +0x202
==================
    testing.go:1092: race detected during execution of test
--- FAIL: TestEqual (0.00s)

=== CONT  
    testing.go:1092: race detected during execution of test
FAIL
@lemire
Copy link
Member

lemire commented May 27, 2021

The runContainer16 equal() function is single threaded. It does not seem possible that it suffers from a data race.

The code provided in this issue modifies the data structure in two distinct threads, which will indeed introduce a data race if one is not careful...

The core issue in this instance is that roaring will compute the cardinality of run containers and cache it as needed. I do not much like this design to be honest.

@lemire
Copy link
Member

lemire commented May 27, 2021

@beiwei30 Can you review #306 and tell me what you think?

@lemire
Copy link
Member

lemire commented May 28, 2021

Release 0.7.1 made data races less likely by removing the cache. Closing.

@lemire lemire closed this as completed May 28, 2021
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

No branches or pull requests

2 participants