Skip to content

Process a property source added just before a refresh reads only once, in its order - #14171

Merged
radovanradic merged 2 commits into
5.2.xfrom
fix/14159-add-before-refresh-read-5.2.x
Oct 28, 2026
Merged

radovanradic merged 2 commits into
5.2.xfrom
fix/14159-add-before-refresh-read-5.2.x

Conversation

@radovanradic

@radovanradic radovanradic commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #14159

The 5.2.x version of #14163. It follows the same rule, but keeps the record in a different place, because 5.2.x reads its property sources differently: see How it differs from 4.10.x.

The problem

Since 5.2.0 (#13079), a refresh holds the catalog lock for its whole rebuild, and builds the new catalogs apart from the published ones. Another thread can add a property source just before the refresh copies the registered property sources. When that happens:

  1. The add registers the property source and sees that the environment is running and not reading. Then it waits for the catalog lock.
  2. The refresh's copy includes that property source, so the refresh processes it in its order.
  3. Once the refresh has published, the add gets the lock and processes the same property source again, on top of everything else.

Until the next refresh, a property source of a lower order then overrides ones of a higher order, such as environment variables or system properties.

The fix

  • Snapshots are numbered. Each start or refresh numbers the copy it takes of the registered property sources. The snapshot holds the property sources it took from there and processes, by identity.

  • The catalogs keep the snapshot they were filled from. Before a read processes its property sources, it notes its snapshot on the catalogs it writes into, and clears the one they kept. Once it has processed all of them into those same catalogs, they keep its snapshot.

  • An add skips only what those catalogs already hold. When the add gets the catalog lock, it leaves its property source as processed only if the catalogs it writes into keep a snapshot that:

    • was taken after the add registered its property source;
    • contains this exact instance.

    In every other case the add processes the property source as before.

  • Registration and snapshot can't interleave. An add already registers its property source under the monitor of the registered property sources map, and a read already copies them under it. The add now reads the snapshot count there, and the read counts there. So either the snapshot holds the source, or the add's count already includes the snapshot. No new lock.

The rule is that the snapshot the catalogs keep always describes those catalogs. Because it lives on the catalogs (ResolverState), it is published, kept and dropped with them, with no code at those places:

Where the published catalogs change Snapshot they keep
a refresh that succeeds, at state = writeState the refresh's, if its read processed everything into them
a refresh that fails, at writeState = state unchanged: the published catalogs and their snapshot stay
reset() outside a refresh, which is a plain stop() none: the new catalogs are empty
a start outside a refresh, which writes straight into the published catalogs cleared when it starts processing; the start's once it has processed all
a start whose catalogs are replaced during its read, by a refresh or a stop none on the new catalogs: the start did not note itself on them

A stop() or a refresh inside a refresh resets only the catalogs being built, so the published ones keep their snapshot.

How it differs from 4.10.x

  • Where the record lives. On 4.10.x the environment keeps the snapshot of the last published read, and the resolver tells it when it publishes. On 5.2.x a start does not hold the catalog lock across its read: it processes one property source at a time, straight into the published catalogs. A refresh or a stop on another thread, or from a property source the start reads, can replace those catalogs in the middle. A record kept on the environment could then describe catalogs that the read never filled. Kept on the catalogs, it can't.
  • No new lock. 5.2.x already registers and copies the property sources under the monitor of the map, so registrationLock isn't needed.
  • The add. DefaultEnvironment holds the resolver rather than extending it, so the add calls a new package-private processAddedPropertySource instead of overriding a hook.

What does not change

  • Adds still wait for a refresh, as before (A property source added from another thread during a refresh waits for the whole refresh, and can deadlock with it #14158).
  • An add made once a refresh has copied the registered property sources is still processed on top.
  • A property source that is read, changed and added again is still processed again on top.
  • An add during a refresh that fails is still processed once the refresh has failed.
  • An add that waits across a stop, a stop and a failed start, or a start that a refresh overtakes, still processes its property source.
  • The standalone PropertySourcePropertyResolver never keeps a snapshot.
  • No public or protected signature changes. javap -protected of PropertySourcePropertyResolver and DefaultEnvironment is identical to 5.2.x.
  • No new deadlock. No new lock. The new methods take only the catalog lock, never while the map's monitor is held, which keeps the existing lock order. The add calls getConvention() before it takes the catalog lock, as 5.2.x did before Keep lookups from failing while a property source is added to the running environment #14167.

Tests

AddBeforeRefreshReadsSpec has 9 cases. Each holds the catalog lock through the resolver's diff, lets another thread's add register its property source and wait for the lock, and then runs the refresh, stop or start.

4 reproduce #14159. They fail on 5.2.x, each with the lower-order property source on top, and pass with the fix:

  • an add just before the refresh reads, with refresh() and with refreshAndDiff();
  • an add that waits while a refresh succeeds and the environment is restarted;
  • an add that waits while a refresh succeeds and a second refresh fails.

5 guard existing behaviour. They pass on 5.2.x and with the fix:

  • a source that is read, changed and added again is processed again;
  • an add just before a refresh that fails is processed;
  • an add that waits across a stop is processed;
  • an add that waits across a stop and a failed start is processed;
  • an add that waits across a stop and a start that a refresh overtakes is processed.

Also run, with JDK 25:

  • io.micronaut.context.env.*: 313 pass.
  • :micronaut-context:test: 58 pass (1 skipped).
  • :micronaut-runtime:test: 112 pass (3 skipped).
  • Spotless passes. Checkstyle, which is off for inject, finds nothing new when turned on: the 5 findings in these files already exist on 5.2.x.

A behaviour comparison with 5.2.x

To check that the fix changes nothing besides #14159, I used a local recorder (not committed), as on #14163. It runs 90 scenarios on 5.2.x and on this branch and writes one outcome per scenario:

  • Trigger: refresh() or refreshAndDiff().
  • When the add happens:
  • What is added:
    • a new instance;
    • the registered instance again;
    • the registered instance, changed.
  • Order: below or above a competitor for the same key.
  • Lifecycle: an add that waits while a refresh runs and is then followed by one of:
    • a stop;
    • a stop and a start;
    • a stop and a failed start;
    • a refresh that fails;
    • a stop and a start that a refresh overtakes while it reads.
  • Recorded:
    • the competing key;
    • the added source's own keys, including a generated one;
    • the registered source;
    • the difference that refreshAndDiff() reports;
    • the same values after one more refresh.

The expected differences were written down before the change. The result is exactly those 12 differences, all of them #14159 itself: the cases where the added source has the lower order and 5.2.x processes it a second time on top.

  • 6 for an add just before the read;
  • 3 for a restart;
  • 3 for a refresh that fails.

In each of them, only the competing key changes, to the higher-order value. Everything else, and the state after one more refresh, is identical to 5.2.x. The recorders that #14162 and #14167 used give the same outcomes on 5.2.x and on this branch.

Known limit

Two adds of different instances under one name around a refresh: the first registers X and waits, and the second replaces it with Y before the refresh copies the registered property sources, then waits too. The refresh reads Y in its order, Y's add leaves it as read, and X's add processes X on top. On 5.2.x the two adds processed X and Y on top in whichever order they got the lock, so either could win; now X does. Making the add of a replaced property source process nothing belongs to the refresh redesign. #14163 behaves the same.

With #14167

#14167 merged first, and this branch is rebased on it. Since #14167, an add writes into a copy of the catalogs, new ResolverState(published), and publishes it:

  • the add checks the snapshot first, and goes through that copy only when it processes its property source, so an add that leaves its property source as processed publishes nothing;
  • the copy carries pendingSnapshot and processedSnapshot, as the catalogs it holds are the same ones, written on. Without them, the next add would see no snapshot and process its property source on top again.

Not in this PR

These are related refresh issues on 5.2.x, kept separate so that this PR stays small:

…, in its order

Since 5.2.0 a refresh holds the catalog lock for its whole read. A property source that another
thread added just before the refresh copied the registered ones waited for the lock, was processed
by the refresh in its order, and was then processed again on top once the refresh had published.
Until the next refresh it overrode property sources of a higher order, such as environment
variables or system properties.

A start or a refresh now numbers the snapshot it takes of the registered property sources. Once it
has processed all of them into the catalogs it noted itself on, those catalogs keep its snapshot,
so the snapshot is published, kept and dropped with them: a refresh that fails keeps the published
catalogs and their snapshot, and a stop resets them without one. An add reads the count of
snapshots when it registers its property source, under the monitor of the registered property
sources, which the read also holds for its snapshot. It leaves the property source as processed
when the catalogs it writes into keep a snapshot taken after that, which holds this instance, and
processes it as before otherwise. Adds still wait for a refresh, as before.

Fixes #14159
@radovanradic
radovanradic force-pushed the fix/14159-add-before-refresh-read-5.2.x branch from e405130 to abadbc6 Compare October 27, 2026 15:03

@graemerocher graemerocher 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.

Thanks, this looks correct to me. Keeping the snapshot on ResolverState so it is published, kept and dropped with the catalogs is a good fit for 5.2.x, where a start writes straight into the published catalogs and can have them replaced mid-read. I walked through the cases in the table (successful/failed refresh, reset() inside and outside a refresh, a start overtaken by a stop or refresh, the copy made by addToCopy during a start) and the number > snapshotsWhenRegistered && contains(instance) rule holds in each. The lock order is unchanged: processAddedPropertySource takes only catalogLock, never under the propertySources monitor, and re-entering it in addToCopy is fine. The tests wait on BLOCKED plus registration rather than sleeping, which is what I'd want for this kind of race.

A few small points inline, none blocking.

…ves its snapshot outside a finally

The add logged that it was processing its property source also when the read it waited for had processed it already. It now logs that in the branch that processes, and the skip at debug with the snapshot that processed it. The snapshot's Javadoc says that it keeps the property sources it holds reachable while its catalogs are published.
@sonarqubecloud

Copy link
Copy Markdown

@radovanradic
radovanradic merged commit 5281a8b into 5.2.x Oct 28, 2026
18 checks passed
@radovanradic
radovanradic deleted the fix/14159-add-before-refresh-read-5.2.x branch October 28, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants