easyice commented on code in PR #15631:
URL: https://github.com/apache/lucene/pull/15631#discussion_r2745067254


##########
lucene/core/src/java/org/apache/lucene/util/StringHelper.java:
##########
@@ -458,11 +458,12 @@ public static byte[] randomId() {
     //     what impact that has on the period, whereas the simple ++ (mod 
2^128)
     //     we use here is guaranteed to have the full period.
 
-    byte[] bits;
+    final BigInteger scratch;
     synchronized (idLock) {
-      bits = nextId.toByteArray();
+      scratch = nextId;
       nextId = nextId.add(BigInteger.ONE).and(mask128);
     }
+    byte[] bits = scratch.toByteArray();

Review Comment:
   @dweiss Thanks for reviewing! if we move `byte[] bits = 
nextId.toByteArray()` before the `synchronized` block, two threads could read 
the same `nextId` value before acquiring the lock, resulting in duplicate IDs. 
Is this understanding correct?
   
   It looks like there was no existing test covering the multi-threaded 
scenario for this method, so a test was added.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to