merlimat commented on a change in pull request #7409:
URL: https://github.com/apache/pulsar/pull/7409#discussion_r448328640



##########
File path: 
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/BitSetRecyclable.java
##########
@@ -1197,11 +1199,13 @@ private BitSetRecyclable(Handle<BitSetRecyclable> 
recyclerHandle) {
     }
 
     public static BitSetRecyclable create() {
-        return RECYCLER.get();
+        BitSetRecyclable instance = RECYCLER.get();
+        instance.recycled.set(false);
+        return instance;
     }
 
     public void recycle() {
-        if (recyclerHandle != null) {
+        if (recyclerHandle != null && recycled.compareAndSet(false, true)) {

Review comment:
       This is still not safe. There cannot be a race condition to resolve the 
recycling. 
   
   In this case it can happen: 
    1. `thread-1` calls recycle and CAS succeeds
    2. `thread-2` gets a the object from pool
    3. `thread-3` sees `recycled==false` and does the CAS again, recycle a 
different object that is in use. 
   
   You cannot use Recyclable for objects with a shared ownership. Either change 
the logic to avoid race conditions, or use ref-counting.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to