Skip to content

fix(client): guard fuzzy-watch future against lost and spurious wakeups - #15228

Merged
KomachiSion merged 1 commit into
alibaba:developfrom
daguimu:fix/config-fuzzy-watch-wait-loop
May 25, 2026
Merged

KomachiSion merged 1 commit into
alibaba:developfrom
daguimu:fix/config-fuzzy-watch-wait-loop

Conversation

@daguimu

@daguimu daguimu commented May 24, 2026

Copy link
Copy Markdown
Contributor

What is the purpose of the change

The Future returned by ConfigFuzzyWatchContext#createNewFuture() waits for initialization with a plain if + Object#wait(), and the flag check sits outside the synchronized block:

public Set<String> get() throws InterruptedException, ExecutionException {
    if (!ConfigFuzzyWatchContext.this.initializationCompleted.get()) {
        synchronized (ConfigFuzzyWatchContext.this) {
            ConfigFuzzyWatchContext.this.wait();
        }
    }
    return new HashSet<>(ConfigFuzzyWatchContext.this.getReceivedGroupKeys());
}

markInitializationComplete() sets the flag and then calls notifyAll() exactly once:

public void markInitializationComplete() {
    initializationCompleted.set(true);
    synchronized (this) {
        this.notifyAll();
    }
}

This combination has two classic wait/notify defects:

  • Lost wake-up → permanent hang. If the caller passes the !initializationCompleted check and is descheduled before entering the synchronized block, markInitializationComplete() can run its set(true) + notifyAll() in that window. The caller then enters wait() after the only notifyAll has already fired, so get() blocks forever — initialization completes only once, so no further notification ever arrives.
  • Spurious wake-up → stale result. Object#wait() may return without any notifyAll. With the if form 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 a while loop. Both classes have an identical markInitializationComplete() (flag + notifyAll on this), so this simply aligns the config side with the naming side:

public Set<String> get() throws InterruptedException, ExecutionException {
    synchronized (ConfigFuzzyWatchContext.this) {
        while (!ConfigFuzzyWatchContext.this.initializationCompleted.get()) {
            ConfigFuzzyWatchContext.this.wait();
        }
    }
    return new HashSet<>(ConfigFuzzyWatchContext.this.getReceivedGroupKeys());
}

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 its notifyAll in between), and the while loop re-checks the flag so a spurious wake-up cannot leak an incomplete result.

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. 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 a while loop guarded by initializationCompleted
  • client/.../ConfigFuzzyWatchContextTest.java — new regression test

Verifying this change

Red/green verified:

  • With the old if form, the new test fails — a spurious notifyAll() (flag still false) makes get() return early (expected: <false> but was: <true>).
  • With the while form, the test passes — get() keeps waiting through the spurious wake-up and only returns after a real markInitializationComplete().
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

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it).
  • Format the pull request title like [ISSUE #123] .... Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction. A red/green regression test is included in ConfigFuzzyWatchContextTest.
  • Run mvn -B clean package apache-rat:check checkstyle:check spotbugs:check -DskipTests to make sure basic checks pass.

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
@github-actions

Copy link
Copy Markdown

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API、新增配置参数、修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@KomachiSion
KomachiSion merged commit d8d1992 into alibaba:develop May 25, 2026
7 checks passed
@KomachiSion KomachiSion added this to the 3.2.2 milestone May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants