Skip to content

Keep lookups from failing while a property source is added to the running environment - #14167

Merged
radovanradic merged 3 commits into
5.2.xfrom
fix/14140-add-off-to-the-side-5.2.x
Oct 27, 2026
Merged

radovanradic merged 3 commits into
5.2.xfrom
fix/14140-add-off-to-the-side-5.2.x

Conversation

@radovanradic

Copy link
Copy Markdown
Contributor

Fixes #14140.

This is the second of the small 5.2.x refresh PRs, one issue each, after #14162. It changes PropertySourcePropertyResolver by about 60 lines of code, and one call in DefaultEnvironment.addPropertySource.

Why

The resolver keeps its properties in three catalogs. Each catalog is an array of LinkedHashMaps, one per first letter of the key.

A refresh writes into a new state and publishes it in one step (#13079). An add on a running environment, outside a start or refresh, does not: addPropertySource → processPropertySource writes straight into the published maps, under the catalog lock. Lookups read those maps without the lock, and several of them iterate a whole first-letter map:

  • getProperty(name, Map/Properties/…);
  • getProperties(name);
  • containsProperties(name);
  • getPropertyEntries(name);
  • getPropertyPathMatches(…).

If another thread adds a key with the same first letter meanwhile, the lookup fails with ConcurrentModificationException, or it returns a mix of values from before and after the add. Almost all framework configuration starts with micronaut., so any runtime add can hit almost any of these lookups. A configuration watcher such as the Consul client's makes such adds, and configuration binding goes through these lookups, for example @ConfigurationProperties beans with Map fields.

Change

  1. The add writes into a copy and publishes it in one step. Under the catalog lock, addToCopy copies the published state, the add processes its property source into the copy, and the copy is published. A lookup on another thread sees the properties as they were before the add, or as they are after it.
  2. Only the first-letter maps are copied. The copy shares the three arrays' maps with the published state, and a map is copied before the add's first write into it. Each add costs three arrays of 58 references, plus the maps it writes to.
  3. The copy keeps what the add relied on:
    • Remembered random values (Keep ${random.*} values stable when a property source is processed #14109): the copy shares them. Before the copy is made, the published state's revision is made odd, as it was while an add wrote into it, so a lookup still reading the old state cannot replace what is remembered for the new one.
    • RAW's record of the lists it owns (rawOwnedBases): the copy keeps it.
    • The adding thread: it reads the copy while it adds, so a property source whose code looks up properties sees what it has added so far.
    • A failing add: it publishes what it wrote before it failed, as before, and the exception reaches the caller.
  4. A refresh started by a property source while it is added behaves as before. It publishes as any other refresh does, and the add goes on in a copy of what it published. If the refresh fails, the add goes on in its own copy. refreshAndDiff() there compares against what the add has written so far.

What stays as on 5.2.x

Tests

New AddWhileLookingUpSpec

A lookup is parked in the middle of its iteration by a value whose toString() parks once armed, as in the issue's reproducer. The add then runs, and the lookup is released.

Case 5.2.x This PR
getProperty(name, Map), getProperty(name, Properties) and getProperties(name), parked while another thread adds a key of the same first letter, return the properties from before the add fail pass
containsProperties, getPropertyEntries and getPropertyPathMatches in a loop for 2 seconds while another thread keeps adding keys of the same first letter. They iterate only keys, so they cannot be parked fails passes
A property source sees the properties it has added so far while it is added passes passes
A random value read while a property source is added is the value lookups return after it passes passes
A property source that fails while it is added leaves what it wrote before it failed passes passes
A refresh run by a property source while it is added, and failing, leaves the earlier properties and what the add wrote passes passes
refreshAndDiff() run by a property source while it is added reports what the refresh changed passes passes
An element of a list that an added indexed key does not write keeps its own (case-insensitive) map passes passes
A list built from indexed keys keeps the raw key and origin of its entry when an added property source adds to it passes passes
A list, or a map, under an indexed key that an added property source writes into reads the same through foo and through foo[0] passes passes

The cases that pass on 5.2.x guard behaviour this PR must keep. The last five rows came from review rounds of earlier versions of this change. Each guards something a version got wrong; the last one was reproduced by the reviewer.

Local comparison with 5.2.x (not committed)

  • Scenario recorder, 126 scenarios.

    • When: the lookup is parked during the add, the add is parked while the lookup runs, or the add comes first.
    • What the add does:
      • adds a new key of the same first letter;
      • writes an indexed key into an existing list;
      • replaces a value;
      • adds the same instance again;
      • fails halfway;
      • writes an indexed key over an earlier source's list;
      • brings a source with both a list and an indexed key into it.
    • Lookups: Map, Properties, getProperties, a list of maps, a plain value and a random value.
    • Recorded: the lookup's result, then the values after the add, after a refresh, and after removing the source and refreshing.

    Compared with 5.2.x, the only differences are the 9 scenarios where a map lookup runs during the add. 5.2.x fails there with ConcurrentModificationException, or returns a mix of values from before and after the add. This PR returns the values from before the add. Everything else is identical, including the list scenarios. The recorder of Keep a property source added while the environment loads its configuration files after the next refresh #14162 also gives the same outcomes on this branch as on 5.2.x.

  • Randomized stress test, 500 seeds. It runs concurrent adds, removes, refreshes and stop/starts with a reader, then compares the values with a fresh environment.

    Failure 5.2.x This PR
    ConcurrentModificationException in a lookup 33 0
    wrong list value after stop(); a conversion service race that 5.2.x also has 0 1

    The counts are timing dependent and vary between runs. The list race also shows up in some 5.2.x runs; with the exceptions gone, more seeds reach that check.

  • Local runs with JDK 25, all passing:

    • micronaut-inject: 531 tests;
    • micronaut-context: 58 tests (1 skipped);
    • micronaut-runtime: 112 tests (3 skipped).

History

The branch has three commits; I suggest squashing them:

  1. The fix as first written. It also copied the lists and maps inside values.
  2. A merge of 5.2.x after Keep a property source added while the environment loads its configuration files after the next refresh #14162.
  3. A narrowing to the first-letter maps. A review showed that copying those lists breaks the consistency between foo and foo[0] that 5.2.x keeps by writing one shared list in place. That belongs to An indexed property such as app.list[0] modifies the list held by the property source that declares the list #13914.

…ning environment

A property source added to a running environment, outside a start or refresh, was processed straight into the property maps that lookups read without a lock, so a lookup that iterated them, such as getProperty(name, Map), getProperties or containsProperties, could fail with ConcurrentModificationException or see the add half done. The add now writes into a copy of the published state, which shares the maps and lists it does not write and copies the ones it writes before the first write, and publishes the copy in one step, as a refresh does. A lookup sees the properties as they were before the add or as they are after it. Random values remembered so far, a refresh run by a property source while it is added, and an add that fails halfway behave as before. As the add no longer writes into a list or map that a property source supplied, an indexed key of an added property source no longer changes that property source's own list.

Fixes #14140
…efore

Copying them before an add wrote into them meant no longer writing a property source's own list (#13914), and 5.2.x keeps a list under an indexed key consistent with the list it was expanded into only by writing that one list in place: with foo[0]=[a,b] and an added foo[0][1]=x, foo read [[a,x]] while foo[0] still read [a,b]. The add now copies only the first-letter maps, which is what the lookups listed in #14140 iterate, and writes the lists and maps inside the values in place, as 5.2.x does. #13914 is left for its own change.

Refs #14140
@sonarqubecloud

Copy link
Copy Markdown

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.

2 participants