ably-java icon indicating copy to clipboard operation
ably-java copied to clipboard

minor race condition in AblyBase.InternalChannels.get

Open jahlbornG opened this issue 4 years ago • 0 comments

This is broken out of #649 . The underlying map is now thread safe, but there is still a minor race condition in https://github.com/ably/ably-java/blob/main/lib/src/main/java/io/ably/lib/rest/AblyBase.java#L114 .

if two threads call get() for a channel which does not currently exist in the map at the same time, they will get two different instances (both will find nothing there currently, both will create a new instance and put it in the map). for the "rest" client, this is a relatively minor issue since the channel doesn't do much.

this could be handled the same way as AblyRealtime, although a cleaner concurrent solution could use this pattern:

Foo f = map.get(key);
if(f == null) {
  Foo f = new Foo();
  Foo oldF = map.putIfAbsent(key, f);
  if(oldF != null) {
    f = oldF;
  }
}

┆Issue is synchronized with this Jira Bug by Unito

jahlbornG avatar Mar 18 '21 12:03 jahlbornG