Skip to content

Use a ConcurrentDictionary from Swiclops to prevent race conditions on Session.users

Charles Wright requested to merge cvwright/46-concurrent-dictionary into main

We were having an intermittent crash on startup of the Circles app, because sometimes we would have concurrent access to the Session.users dictionary. Attempting to read and write it at the same time would cause the app to crash.

Previously we tried a simple hack where we pre-populated the most likely immediate User (our own User) into the dict. That may have reduced the frequency of the crashes -- or maybe not.

This MR should end the crashes once and for all.

Here we bring in the ConcurrentDictionary struct from Swiclops. Borrowing ideas that I first saw used in Go, we use a DispatchQueue to serialize concurrent access to a simple dictionary. Then to reduce blocking and improve performance, the ConcurrentDictionary uses a collection of those serialized queue dictionaries and shards entries across them by hashing the key. This reduces the probability that two callers will attempt to access the same serialized dictionary at the same time, reducing the chance that the 2nd caller will have to block while the first access completes.

Merge request reports

Loading