Repository navigation
fix(client): guard fuzzy-watch future against lost and spurious wakeups - #15228
Merged
KomachiSion merged 1 commit intoMay 25, 2026
Merged
Conversation
The `Future` returned by `ConfigFuzzyWatchContext#createNewFuture()`
waited for initialization with a plain `if` + `Object#wait()`, and the
flag check sat outside the `synchronized` block:
if (!initializationCompleted.get()) {
synchronized (this) {
this.wait();
}
}
return new HashSet<>(getReceivedGroupKeys());
This has two concurrency defects, because `markInitializationComplete()`
sets the flag and then calls `notifyAll()` exactly once:
- Lost wake-up: if the caller passes the `!initializationCompleted`
check and is then descheduled before entering the synchronized block,
`markInitializationComplete()` can run its `set(true)` + `notifyAll()`
in between. The caller then enters `wait()` after the only `notifyAll`
has already fired, so `get()` blocks forever.
- Spurious wake-up: `wait()` may return without any `notifyAll`. With
the `if` form the method falls through and returns a result set that
reflects an incomplete initialization.
The sibling class `NamingFuzzyWatchContext#createNewFuture().get()`
already uses the correct canonical form -- it takes the lock first and
re-checks the flag in a `while` loop -- so this aligns the config side
with the naming side:
synchronized (this) {
while (!initializationCompleted.get()) {
this.wait();
}
}
Scope: only the no-timeout `get()` is changed. The timed
`get(long, TimeUnit)` overload keeps its `if` form in both classes; it
re-checks the flag after waking and throws `TimeoutException` when
initialization is still incomplete, so a spurious wake-up cannot leak an
incomplete result there. Keeping it untouched preserves symmetry between
the two context classes.
## Brief changelog
- `client/.../ConfigFuzzyWatchContext.java` -- take the monitor first and
wait in a `while` loop guarded by `initializationCompleted`
- `client/.../ConfigFuzzyWatchContextTest.java` -- new regression test:
a spurious `notifyAll` (flag still false) must not let `get()` return,
and a real `markInitializationComplete()` must release it
## Verifying this change
- Red/green verified: with the old `if` form the new test fails
(`get()` returns early on the spurious `notifyAll`); with the `while`
form it passes.
- `mvn -pl client -am clean compile spotbugs:check checkstyle:check apache-rat:check spotless:check -DskipTests` -- passes
- `mvn -pl client test -Dtest=ConfigFuzzyWatchContextTest` -- 2 passed
|
Thanks for your this PR. 🙏 感谢您提交的PR。 🙏 |
KomachiSion
approved these changes
May 25, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
What is the purpose of the change
The
Futurereturned byConfigFuzzyWatchContext#createNewFuture()waits for initialization with a plainif+Object#wait(), and the flag check sits outside thesynchronizedblock:markInitializationComplete()sets the flag and then callsnotifyAll()exactly once:This combination has two classic
wait/notifydefects:!initializationCompletedcheck and is descheduled before entering thesynchronizedblock,markInitializationComplete()can run itsset(true)+notifyAll()in that window. The caller then enterswait()after the onlynotifyAllhas already fired, soget()blocks forever — initialization completes only once, so no further notification ever arrives.Object#wait()may return without anynotifyAll. With theifform the method falls through and returns a result set that reflects an incomplete initialization.How
The sibling class
NamingFuzzyWatchContext#createNewFuture().get()already uses the correct canonical form — acquire the monitor first, then re-check the flag inside awhileloop. Both classes have an identicalmarkInitializationComplete()(flag +notifyAllonthis), so this simply aligns the config side with the naming side:Taking the lock before the check closes the lost-wake-up window (the waiter holds the monitor across check-and-wait, so
markInitializationComplete()cannot slip itsnotifyAllin between), and thewhileloop re-checks the flag so a spurious wake-up cannot leak an incomplete result.Scope
Only the no-timeout
get()is changed. The timedget(long, TimeUnit)overload keeps itsifform in both classes: it re-checks the flag after waking and throwsTimeoutExceptionwhen initialization is still incomplete, so a spurious wake-up cannot leak an incomplete result there. Leaving it untouched preserves symmetry between the two context classes; tightening the timed path would be a separate change.Brief changelog
client/.../ConfigFuzzyWatchContext.java— acquire the monitor first and wait in awhileloop guarded byinitializationCompletedclient/.../ConfigFuzzyWatchContextTest.java— new regression testVerifying this change
Red/green verified:
ifform, the new test fails — a spuriousnotifyAll()(flag stillfalse) makesget()return early (expected: <false> but was: <true>).whileform, the test passes —get()keeps waiting through the spurious wake-up and only returns after a realmarkInitializationComplete().Follow this checklist to help us incorporate your contribution quickly and easily:
[ISSUE #123] .... Each commit in the pull request should have a meaningful subject line and body.ConfigFuzzyWatchContextTest.mvn -B clean package apache-rat:check checkstyle:check spotbugs:check -DskipTeststo make sure basic checks pass.