[BUG] ExtensionLoader.getJoins() fast-path returns null / throws NPE under concurrent init
- Dominant language
- Java
- Stars
- 8.8k
- Forks
- 3.1k
- Avg merge
- 7d 1h
- Merged PRs (30d)
- 85
Description
- Severity: Medium
- Location:
`shenyu-spi/src/main/java/org/apache/shenyu/spi/ExtensionLoader.java:174-194,55,331-369`
-
Description:
`getJoins()` takes a fast path when `extensionClassesEntity.size() == cachedInstances.size()`, returning `cachedInstances.values().stream().sorted(HOLDER_COMPARATOR).map(e -> e.getValue())`. A `Holder` is inserted via `putIfAbsent` (line 153) *before* its `value`/`order` is populated inside the `synchronized(objectHolder)` block in `createExtension`. If thread A has just done `putIfAbsent` for the last name (so size reaches N) but not yet `setValue`/`setOrder`, a concurrent thread B calling `getJoins()` sees the size match, takes the fast path, reads `getValue()==null`/`getOrder()==null`. `HOLDER_COMPARATOR = Comparator.comparing(Holder::getOrder)` then throws `NullPointerException` (null `Integer` key); absent that, the returned list contains a `null` element.
-
Impact:
Concurrent `getJoins()` (dynamic plugin/route loading) can throw NPE or return a list with `null`, causing NPE at the call site.
-
Suggested fix:
Populate `value`/`order` before inserting the Holder, or gate the fast path on a `volatile boolean allInitialized` set after the slow path completes; or use `ConcurrentHashMap.compute` so a Holder is never observable half-built.
-
Confidence: Medium
---
_Identified during the 2026-08-02 deep re-scan; full list in [`docs/scan2-2026-08-02/06-medium-tiers.md`](docs/scan2-2026-08-02/06-medium-tiers.md)._
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in shenyu-spi/src/main/java/org/apache/shenyu/spi/ExtensionLoader.java, especially getJoins(), createExtension(), and the Holder initialization around the cited lines. Trace how putIfAbsent, setValue, and setOrder interact during concurrent initialization, then add coverage for concurrent getJoins() calls. Done means the fast path cannot expose null values or orders and no NPE occurs under the described race.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100