Repository navigation
Keep lookups from failing while a property source is added to the running environment - #14167
Merged
Merged
Conversation
…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
|
graemerocher
approved these changes
Oct 27, 2026
This was referenced Oct 27, 2026
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 #14140.
This is the second of the small 5.2.x refresh PRs, one issue each, after #14162. It changes
PropertySourcePropertyResolverby about 60 lines of code, and one call inDefaultEnvironment.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→processPropertySourcewrites 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 withmicronaut., 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@ConfigurationPropertiesbeans withMapfields.Change
addToCopycopies 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.rawOwnedBases): the copy keeps it.refreshAndDiff()there compares against what the add has written so far.What stays as on 5.2.x
foo[0],foo[0].bar) are written in place, as now. A lookup of such a list, made while an add expands into the same list, can therefore still fail. That case is left to An indexed property such as app.list[0] modifies the list held by the property source that declares the list #13914: the comment there describes it and what a fix has to keep consistent: An indexed property such as app.list[0] modifies the list held by the property source that declares the list #13914 (comment)stop(): a start still writes straight into the published state, so such a lookup can still fail the same way. Changing that would change when a stop can interrupt a start (Environment#stop()orstart()called from another thread during a refresh waits for the whole refresh, and can deadlock with it #14141).PropertySourcePropertyResolver.addPropertySource()still writes in place.javap -protectedofPropertySourcePropertyResolverandDefaultEnvironmentis identical to 5.2.x.Tests
New
AddWhileLookingUpSpecA 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.getProperty(name, Map),getProperty(name, Properties)andgetProperties(name), parked while another thread adds a key of the same first letter, return the properties from before the addcontainsProperties,getPropertyEntriesandgetPropertyPathMatchesin a loop for 2 seconds while another thread keeps adding keys of the same first letter. They iterate only keys, so they cannot be parkedrefreshAndDiff()run by a property source while it is added reports what the refresh changedfooand throughfoo[0]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.
Map,Properties,getProperties, a list of maps, a plain value and a random value.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.
ConcurrentModificationExceptionin a lookuplistvalue afterstop(); a conversion service race that 5.2.x also hasThe counts are timing dependent and vary between runs. The
listrace 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:
fooandfoo[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.