nicktindall opened a new issue, #16861: URL: https://github.com/apache/lucene/issues/16861
### Description `SegmentCommitInfo.sizeInBytes()` caches its result in a volatile field whose `-1` value means “not computed yet”: https://github.com/apache/lucene/blob/12d316cb85463e4bd0bce550077216a7c5dbb9b1/lucene/core/src/java/org/apache/lucene/index/SegmentCommitInfo.java#L269-L280 `generationAdvanced()` invalidates that cache by assigning -1. https://github.com/apache/lucene/blob/12d316cb85463e4bd0bce550077216a7c5dbb9b1/lucene/core/src/java/org/apache/lucene/index/SegmentCommitInfo.java#L457-L460 If a thread computes and stores a size, and another thread advances the generation before the first thread executes the final volatile read, then `sizeInBytes()` returns -1 instead of a size. There is also a separate best-effort-cache race: if invalidation occurs while a size is being computed, the completed computation can overwrite the invalidation with a size derived from the earlier segment state. The proposed small fix is to load the cached value into a local variable, compute into that local when needed, publish it once, and return the local. This guarantees that a successful call never returns the -1 sentinel: ```java long size = sizeInBytes; if (size == -1) { size = 0; for (String fileName : files()) { size += info.dir.fileLength(fileName); } sizeInBytes = size; } return size; ``` This does not make `SegmentCommitInfo` thread-safe or guarantee that a size computed concurrently with mutations is current. In particular, `files()` still reads mutable collections and callers requiring a consistent segment state must retain the appropriate writer-side synchronization. It only restores the intended property that callers never receive the cache sentinel as a size. ### Version and environment details 10.5.2 -- 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]
