Skip to content

fix(zset): acquire write lock in RemoveRangeByScoreWithOpt - #268

Open
bodapatisaikrishna wants to merge 1 commit into
bytedance:mainfrom
bodapatisaikrishna:fix/removerangebyscore-lock
Open

bodapatisaikrishna wants to merge 1 commit into
bytedance:mainfrom
bodapatisaikrishna:fix/removerangebyscore-lock

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown

Problem

Float64Set.RemoveRangeByScoreWithOpt deletes elements from the skiplist and dictionary via z.list.DeleteRangeByScore(min, max, opt, z.dict). However, it acquired a read lock (z.mu.RLock()) instead of an exclusive write lock (z.mu.Lock()).

When RemoveRangeByScore is executed concurrently, multiple goroutines enter DeleteRangeByScore simultaneously, resulting in fatal error: concurrent map writes or data races.

Fixes #234.

Solution

  • Acquire z.mu.Lock() / defer z.mu.Unlock() in RemoveRangeByScoreWithOpt to serialize modifications to the skiplist and map.
  • Add TestFloat64SetRemoveRangeByScoreConcurrently in collection/zset/zset_test.go to test concurrent executions of RemoveRangeByScore under -race.

Verification

All tests in collection/zset pass with -race:

$ go test -v -race ./collection/zset
PASS
ok  	github.com/bytedance/gopkg/collection/zset	1.421s

RemoveRangeByScoreWithOpt modifies the underlying skiplist and dict
via DeleteRangeByScore, but was previously acquiring a read lock (RLock)
instead of an exclusive write lock (Lock). This caused data races and
concurrent map write panics when RemoveRangeByScore was called concurrently.

Acquire Lock/Unlock instead, and add TestFloat64SetRemoveRangeByScoreConcurrently
to verify concurrent safety.

Fixes bytedance#234

Signed-off-by: bodapatisaikrishna <saikrishnabodapati@gmail.com>
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.

RemoveRangeByRank lock by rmutex

1 participant