Skip to content

Keep ${random.*} values stable when a property source is processed - #14109

Merged
radovanradic merged 1 commit into
5.2.xfrom
fix/random-values-5.2.x
Oct 23, 2026
Merged

radovanradic merged 1 commit into
5.2.xfrom
fix/random-values-5.2.x

Conversation

@radovanradic

@radovanradic radovanradic commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #13913. The 4.10.x counterpart is #14108.

Why

A property whose value holds a random expression (${random.uuid}, ${random.port}, …) is meant to resolve to the same value on every lookup until the environment is refreshed. The cache of resolved placeholders was what kept it stable.

Since 5.2.0, #12864 clears that cache after every property source the resolver processes. With #14080 (5.2.18) a cached value is also only used for the revision it was read for, and processing a property source changes the revision. So the next lookup after any property source has been processed resolves the expression again and draws a new value.

Since when, by symptom

Symptom 4.10.26 4.10.32 5.1.14 5.2.0 – 5.2.19 This PR
A PropertySourcePropertyResolver returns another random value once an unrelated property source is added (issue, step 1) same value same value same value new value same value
A property source that reads a random property while the property sources are processed gets a value that later lookups do not return (issue, step 2), also through an Environment on start and on every refresh same value same value same value different value same value
The same for a text that also names a property, such as http://${app.host}:${random.port}/ same value different value same value different value same value
The same for random expressions in a list, such as ['${random.uuid}'] same value different value same value different value same value
The same for a random fallback, such as ${app.port:random.port:8080} with app.port not set same value different value same value different value same value

Measured with the issue's steps and probes on each version. #12864 is in every 5.2.x release and in no 5.1.x release. 4.10.32 brought the same cache clear to 4.10.x, with a memory for texts made only of random expressions; #14108 covers the last three rows on 4.10.x. The current 5.3.x branch behaves like 5.2.x on the issue's steps.

These behave as before, on every version above:

  • a refresh draws the random values again;
  • adding or removing a property source on a running environment draws them again, since the environment resets the resolver's caches;
  • where there is no property to remember a value for, such as getProperties() or a placeholder resolved directly, a new value is drawn every time.

Change

  • RandomPropertyExpressionResolver remembers what it draws. It implements the new internal RememberingPropertyExpressionResolver: a PropertyExpressionResolver that DefaultPropertyPlaceholderResolver passes a memory for each expression of a placeholder it resolves for a property. Other expression resolvers are called as before.
  • PropertySourcePropertyResolver knows nothing of random values. When it resolves the placeholders of a property, it passes where the text was found: the name it was looked up by, and the index of a list element or the key of a map value in a list. The places are kept as structured keys, so map keys such as 1 and "1", or a key id[0] next to a list id, stay apart.
  • The memory is kept in the resolver's state, by that place and by the position of the expression in the text: its placeholder, and its place in a fallback chain. A later lookup that finds the same text there gets the same values. If a property source has replaced the text, the new text's expressions are drawn again.
  • The placeholders are resolved as before, expression by expression. So a random fallback such as ${app.port:random.port:8080} is only drawn when app.port is not set, and app.port still wins once it is. A random expression that cannot be resolved fails as before, after a missing property before it, and nothing is remembered for it.
  • The value drawn is remembered, not a converted one, and each lookup converts it to the type it asks for: ${random.uuid}, or ${missing:random.uuid:default}, read as an Object is a UUID, as before.
  • A random expression reads no property, so a property source added later cannot make its value out of date. The rest of a text, such as ${app.host} above, is resolved again by every lookup that does not find the value cached, so it follows the property sources.
  • Like the caches with Keep the property caches from holding a value read before a property source was added #14080's revisions, the memory only takes a new text from a lookup that read the catalogs as they still are, from the thread that is processing a property source and holds the catalog lock, or when nothing is remembered there yet. A lookup that a replaced text overtakes cannot replace what a later lookup has remembered. The check runs inside ConcurrentHashMap.compute, so another lookup at the same place cannot get in between.
  • addPropertySource and removePropertySource on a running environment clear the memory, as they cleared the cache before 5.2.0. They clear it before they change the revision, so a lookup that reads the new revision finds the memory already cleared, and the value it caches is the one the memory keeps.
  • The memory belongs to the resolver's state, which a refresh replaces. A lookup that Fix environment refresh issues that remain after the 5.2.16–5.2.18 refresh fixes #14106 runs again on the state it started with finds the values it remembered there.

Memory

The original incident was an OutOfMemoryError under a refresh every minute. The memory holds one entry per looked-up name, or list element, whose text holds a random expression: the same names the placeholder cache keeps. A refresh replaces it with the state.

Probe: 5,000 refreshAndDiff() with -Xmx64m. Between the refreshes, a property source is replaced by the same name with a different random text each time, and property sources are added and removed by eight names. A second thread looks the random properties up without pause.

Entries Heap after GC Lookups Failures
5.2.18 – 5–6 MB, flat 206M 0
This PR 6 at every sample (the 6 random names looked up) 5–6 MB, flat 193M 0

Tests

  • New in PropertySourcePropertyResolverSpec:
    • the issue's two steps, plus a property source that replaces the random text;
    • a text that also names a property keeps its random values while the property follows a later property source, and a replaced text gets new ones;
    • random expressions in a list and in a map in a list, with the declared list and map left unmodified, and a list element replaced by a later property source getting a new value while the others keep theirs; a list in a list is returned as declared, as before;
    • the keys of a map in a list keep their own random values, also keys that read the same;
    • a lookup of a list that a property source adds to meanwhile gets the list it read, without a ConcurrentModificationException, also when the list holds no random expression;
    • a random expression that a placeholder falls back to keeps its value, and the property named before it still wins once set;
    • a random fallback is not drawn while the property named before it is set, so an invalid one cannot fail the lookup, and it keeps the type of its value, also in a list;
    • random expressions are found as placeholders are resolved (case, spaces, a default), a text with an incomplete placeholder still fails as before, and a missing property before an invalid random expression is still the error reported;
    • a lookup that a replaced text overtakes, stopped after it has read the old text, does not replace what a later lookup remembered;
    • a lookup that starts while a property source replaces the text, and finishes before that property source is done, does not replace what the property source read;
    • a lookup overtaken by an unrelated property source gets the value later lookups return;
    • a lookup made while the memory is cleared, right after the revision changes, gets the value that later lookups of every type return.
  • New in DefaultEnvironmentSpec:
    • a property source that reads a random property while the environment reads its property sources, on start and on refresh, for a random text and for one that also names a property, with a new value drawn by the refresh;
    • a property source added to or removed from a running environment draws new random values, also for a text that names a property and in a list, which every later lookup then gets.
  • The tests of new behaviour fail on 5.2.x, and the race tests fail on earlier versions of this change. The ones that check behaviour kept from before (a list in a list, an incomplete placeholder, a random fallback not drawn while the property before it is set, a property source added or removed at run time) pass on 5.2.x as well.
  • Local runs with JDK 25, all passing:
    • micronaut-inject: 510 tests;
    • micronaut-inject-java: 5,370 tests (9 skipped);
    • micronaut-context: 58 tests (1 skipped);
    • micronaut-runtime: 112 tests (3 skipped);
    • checkstyleMain for inject, which the build disables, run with it enabled: no new violation.


if (value != null && entries != null) {
// remembered apart from the placeholder cache, which a processed property source invalidates
value = withRandomValues(currentState, name, value, lookupRevision);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the wrong fix IMO, right now the design of PropertySourcePropertyResolver is such that it has no awareness of "random" and what it means.

this is encapsulated by RandomPropertyExpressionResolver which implements PropertyExpressionResolver

so RandomPropertyExpressionResolver needs to take part in remembering these values such that the implementation split is correct.

This change hard codes of randomness and duplicates all the patterns defined in RandomPropertyExpressionResolver here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll rework it so that RandomPropertyExpressionResolver does the remembering and PropertySourcePropertyResolver knows nothing about random values:

  • RandomPropertyExpressionResolver remembers what it draws, per placeholder and per expression of a fallback chain, and converts the remembered value to the requested type on each lookup.
  • It can't remember by the expression alone, since a=${random.port} and b=${random.port} would then get the same port. So PropertySourcePropertyResolver passes a context through DefaultPropertyPlaceholderResolver to the expression resolvers, through an internal hook: where the text was found (the property name, and the index of a list element or the key of a map value), and a memory that a refresh replaces and that adding or removing a property source at run time clears, as before. Other expression resolvers are called as they are now.
  • PropertySourcePropertyResolver no longer parses anything: fallbacks such as ${app.port:random.port:8080} and the order of errors follow from resolving the placeholders as usual.

#14108 (4.10.x) gets the same change, which also moves the random memory that #13896 added to PropertySourcePropertyResolver on 4.10.x into RandomPropertyExpressionResolver.

Let me know if you had a different split in mind.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked along these lines. RandomPropertyExpressionResolver now remembers what it draws, through a new internal RememberingPropertyExpressionResolver that DefaultPropertyPlaceholderResolver passes a memory for each expression. PropertySourcePropertyResolver only passes where the text was found and keeps the memory with its state; it no longer knows about random values or parses placeholders. The description is updated.

Since 5.2.0 processing a property source clears the placeholder cache, which was what kept a
random value stable. A lookup after any property source had been processed drew a new value,
and a property source that read a random property while the property sources were processed,
when the environment starts or refreshes, got a value that later lookups did not return.

As on 5.1.x, the values now stay the same. RandomPropertyExpressionResolver remembers what it
draws, through the new internal RememberingPropertyExpressionResolver. When the property resolver
resolves the placeholders of a property, it passes such a resolver a memory for each expression:
kept in the state of the resolver by the place of the text, the name it was looked up by and its
place in a list or in a map in a list, and by the position of the expression in the text. The
property resolver itself knows nothing of random values, and the placeholders are resolved as
before. A random fallback, such as ${app.port:random.port:8080}, is only drawn when the property
before it is not set, and keeps the type of its value. A text that also names a property, such as
http://${app.host}:${random.port}/, keeps its random values while the property is resolved again.

The memory is kept when a property source is processed, drawn again when the text changes, and
forgotten when a property source is added to or removed from a running environment, as before
5.2.0. A refresh publishes another state, so it draws the random values again, as before. The
state holds one entry per looked-up name or list element whose text holds a random expression.

Like the caches, the memory only takes a new text from a lookup that has read the catalogs as
they still are, from the thread that is processing a property source, or when nothing is
remembered yet, so a lookup that a replaced text overtakes does not replace what a later lookup
has remembered. A running environment forgets the values before it changes the revision, so no
lookup caches a value for the new revision that is forgotten after it.

Fixes #13913

@dstepanov dstepanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked bacaa2a: both previously reported fallback regressions are fixed. All 286 environment tests pass, including the original three regression probes. No new actionable findings.

@sonarqubecloud

Copy link
Copy Markdown

@radovanradic
radovanradic merged commit a387a86 into 5.2.x Oct 23, 2026
18 checks passed
@radovanradic
radovanradic deleted the fix/random-values-5.2.x branch October 23, 2026 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants