windwoodfirehill opened a new pull request, #16499:
URL: https://github.com/apache/dubbo/pull/16499
## What is the purpose of the change
#14056 replaced `synchronized (cachedClasses)` with a `ReentrantLock` in
`ExtensionLoader#getExtensionClasses`, because loading extension classes blocks
on a `CountDownLatch` (`ClassLoaderResourceLoader#loadResources`) and that pins
the carrier thread of a virtual thread on JDK 21.
That fix only covers callers that reach `getExtensionClasses()` without
holding a monitor (e.g. `getSupportedExtensions()`). The two most common entry
points still create the instance inside a `synchronized` block:
- `getExtension(name)` → `synchronized (holder)` → `createExtension` →
`getExtensionClasses()`
- `getAdaptiveExtension()` → `synchronized (cachedAdaptiveInstance)` →
`createAdaptiveExtension` → `getExtensionClasses()`
So the first `getExtension(name)` of an SPI from a virtual thread still pins
the carrier.
Reproduced on JDK 21.0.12 with `-Djdk.tracePinnedThreads=full` (the SPI's
classes are not loaded before the virtual thread starts):
```
VirtualThread[#38,vt-repro]/runnable@ForkJoinPool-1-worker-1 reason:MONITOR
java.base/java.lang.VirtualThread.park(VirtualThread.java:596)
...
java.base/java.util.concurrent.CountDownLatch.await(CountDownLatch.java:230)
org.apache.dubbo.common.utils.ClassLoaderResourceLoader.loadResources(ClassLoaderResourceLoader.java:58)
org.apache.dubbo.common.extension.ExtensionLoader.loadDirectoryInternal(ExtensionLoader.java)
org.apache.dubbo.common.extension.ExtensionLoader.loadDirectory(ExtensionLoader.java)
org.apache.dubbo.common.extension.ExtensionLoader.loadExtensionClasses(ExtensionLoader.java)
org.apache.dubbo.common.extension.ExtensionLoader.getExtensionClasses(ExtensionLoader.java)
org.apache.dubbo.common.extension.ExtensionLoader.createExtension(ExtensionLoader.java)
org.apache.dubbo.common.extension.ExtensionLoader.getExtension(ExtensionLoader.java)
<== monitors:1
```
| | `getExtension(name)` from a virtual thread | `getSupportedExtensions()`
from a virtual thread |
|---|---|---|
| before | 1 pinned event | 0 (fixed by #14056) |
| after | 0 | 0 |
JDK 24+ no longer pins on `synchronized` (JEP 491), but JDK 21 is still an
LTS in wide use, and this keeps the three locking sites in `ExtensionLoader`
consistent.
## Brief changelog
- `getExtension(String, boolean)`: lock a per-name `ReentrantLock` instead
of `synchronized (holder)`. Granularity is unchanged (one lock per cache key).
- `getAdaptiveExtension()`: lock a `ReentrantLock` instead of `synchronized
(cachedAdaptiveInstance)`.
`ReentrantLock` is reentrant like a monitor, so re-entering on the same
thread behaves as before.
## Verifying this change
- Existing tests pass: `ExtensionLoaderTest`, `ExtensionDirectorTest`,
`ExtensionLoader_Adaptive_Test`,
`ExtensionLoader_Adaptive_UseJdkCompiler_Test`,
`ExtensionLoader_Activate_Test`, `ExtensionLoader_Compatible_Test` (86 tests).
- Manual reproduction above on JDK 21 before / after the change.
<details>
<summary>Reproducer</summary>
```java
// JDK 21, -Djdk.tracePinnedThreads=full
ApplicationModel app = FrameworkModel.defaultModel().defaultApplication();
ExtensionLoader<StatusChecker> loader =
app.getExtensionLoader(StatusChecker.class);
Thread.ofVirtual().start(() -> loader.getExtension("memory")).join();
```
</details>
---
Problem found by me while reading the SPI code; the patch and this
description were written with Claude Opus 5.5. The pinning trace and the before
/ after table above come from actual runs on JDK 21.0.12.
--
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]