MINOR: make Utils.humanReadableByteCount thread-safe via ThreadLocal DecimalFormat - #23274
MINOR: make Utils.humanReadableByteCount thread-safe via ThreadLocal DecimalFormat#23274shoemoney wants to merge 1 commit into
Conversation
…ecimalFormat Fix verified with RED->GREEN. Static DecimalFormat race in Utils.humanReadableByteCount at Utils.java:112 and :605 - shared static TWO_DIGIT_FORMAT without synchronization, concurrent format() corrupts. Fix: ThreadLocal<DecimalFormat>
|
Hi @shoemoney do you have way to replicate this bug? |
|
Honest answer: no, not deterministically. I wrote a stress harness (12 inputs spanning 1 B to ~7 TB so the shared |
|
A label of 'needs-attention' was automatically added to this PR in order to raise the |
Utils.humanReadableByteCountformats through a singlestatic final DecimalFormatshared by every caller.DecimalFormatis documented asnot thread-safe ("Decimal formats are generally not synchronized. It is
recommended to create separate format instances for each thread"), and
format(double)mutates an internalDigitListon each call.This wraps the formatter in a
ThreadLocalso each thread formatsthrough its own instance; the call site becomes
TWO_DIGIT_FORMAT.get().format(scaled). No behavior change forsingle-threaded callers.
Testing:
:clients:test --tests UtilsTestpasses. I was not able toproduce a corrupted result with a 64-thread stress harness on OpenJDK
21, so this is contract-based hardening rather than a fix for an
observed failure.