WW-5724 Hand out a clone of the cached MessageFormat - #1914
Open
lukaszlenart wants to merge 1 commit into
Open
Conversation
AbstractLocalizedTextProvider caches MessageFormat instances by pattern and locale and returned the cached instance itself from buildMessageFormat. MessageFormat is not thread-safe, so concurrent callers rendering the same message formatted through one shared instance; with a date or time sub-format that produced output for the wrong argument or an exception. buildMessageFormat now returns a clone of the cached instance, so the cached entry is only ever a template and is never formatted directly. MessageFormat.clone() deep-copies the sub-formats. The pattern-parsing cache is kept; measured against a fresh instance per call and against synchronizing on the shared instance, cloning is the cheapest of the three. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
lukaszlenart
marked this pull request as ready for review
September 11, 2026 13:49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes WW-5724
AbstractLocalizedTextProvidercachesMessageFormatinstances by pattern and locale and returned the cached instance itself frombuildMessageFormat.MessageFormatis not thread-safe, so concurrent callers rendering the same message formatted through one shared instance; with a date or time sub-format that produced output for the wrong argument or an exception.buildMessageFormatnow returns a clone of the cached instance, so the cached entry is only ever a template and is never formatted directly.MessageFormat.clone()deep-copies the sub-formats. The pattern-parsing cache is kept.Two tests added to
StrutsLocalizedTextProviderTest: one asserts two lookups of the same pattern no longer return the same instance while the cache still holds one entry; the other renders a{0,date,short}message from four threads with distinct dates and asserts every output matches its own argument. Both failed before the change.Why clone rather than the alternatives: building a fresh instance per call drops the parsing cache the WW-5540 work relies on, and synchronizing on the shared instance serializes all rendering. Measured over 1.2M renders on JDK 17, cloning was the cheapest of the three (176 ms, against 409 ms fresh-per-call and 556 ms synchronized).
No configuration or API change. Applies to both maintenance lines; the 6.x backport follows.
🤖 Generated with Claude Code