AryamannSingh7 opened a new pull request, #16356:
URL: https://github.com/apache/dubbo/pull/16356
## What is the purpose of the change?
Fixes #16178.
When a service subscribes to multiple registries and one of them is **weakly
dependent** (`check="false"`) and **unavailable at startup**, the application
fails to start with:
```
java.lang.NullPointerException: Cannot invoke
"org.apache.dubbo.registry.Registry.getUrl()" because "this.registry" is null
at
org.apache.dubbo.registry.ListenerRegistryWrapper.getUrl(ListenerRegistryWrapper.java:44)
at
org.apache.dubbo.registry.integration.RegistryDirectory.subscribe(RegistryDirectory.java:...)
```
This is a regression: Dubbo `3.2.0`–`3.2.4` start successfully, `3.2.5`+
fail (also reproduced on `3.3.x`).
### Root cause
Under `check=false`, when a registry cannot be created (the registry is
down), `AbstractRegistryFactory#getRegistry` swallows the exception and returns
`null`. `RegistryFactoryWrapper` still wraps this `null` into a
`ListenerRegistryWrapper`, so a null delegate is an **expected state** —
`ListenerRegistryWrapper` already guards `register` / `unregister` /
`subscribe` with `if (registry != null)`.
However:
- `getUrl` / `isAvailable` / `destroy` / `unsubscribe` /
`isServiceDiscovery` / `lookup` were **not** guarded.
- `RegistryDirectory#subscribe` computes a metrics cluster name via
`registry.getUrl().getParameter(...)` unconditionally. That line was introduced
in #12582 (released in `3.2.5`), which is exactly why the NPE appears from
`3.2.5` onward.
### What changed
1. **`ListenerRegistryWrapper`** — complete the existing null-safety:
`getUrl` returns `null`, `isAvailable` / `isServiceDiscovery` return `false`,
`destroy` / `unsubscribe` become no-ops, and `lookup` returns an empty list
when the delegate registry is `null`.
2. **`RegistryDirectory#subscribe`** — guard the metrics cluster-name
computation against a `null` registry URL (no cluster name is reported when the
registry is unavailable). `RegistryEvent#toSubscribeEvent` already tolerates a
`null` name.
### Verification
Added `ListenerRegistryWrapperTest#testNullRegistryIsTolerated`, which
reproduces the exact NPE **without** the fix and passes **with** it.
`:dubbo-registry-api` tests and `spotless:check` pass locally (JDK 21).
## Checklist
- [x] Make sure there is a [GitHub
issue](https://github.com/apache/dubbo/issues/16178) field for the change.
- [x] Write a pull request description that is detailed enough to understand
what the pull request does, how, and why.
- [x] Write necessary unit-test to verify your logic correction.
- [x] Make sure GitHub actions can pass.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]