Real-Time Virtual Thread Pinning Diagnostics, Carrier Starvation Detection, and Telemetry for Java 21+ & Spring Boot 3
With Project Loom in Java 21, high-throughput concurrency is accessible via Virtual Threads (Thread.ofVirtual()). However, in production microservices, virtual thread migrations frequently suffer from two severe anti-patterns:
- Thread Pinning: Synchronized blocks and native JNI methods lock virtual threads to underlying operating system carrier threads (
ForkJoinPool), starving the entire JVM of executor capacity. - Carrier & Pool Starvation: When tens of thousands of virtual threads block simultaneously on undersized database connection pools (such as HikariCP), queues explode, leading to systemic latency spikes and connection deadlocks.
LoomDoctor is a lightweight runtime telemetry and diagnostic library for Java 21+ and Spring Boot 3 that identifies pinning events in real time, inspects connection pool health against virtual thread pressure, and exposes operational insights via Prometheus and a native Actuator endpoint (/actuator/loom-doctor).
When a virtual thread executes blocking I/O inside a synchronized block or native frame, it pins the underlying OS carrier thread, starving the entire carrier pool. LoomDoctor intercepts and flags this immediately:
[LOOM-DOCTOR] 🚨 VIRTUAL THREAD PINNING DETECTED
┌─ Virtual Thread: #4821 [http-nio-8080-exec-14]
├─ Carrier Thread: ForkJoinPool-1-worker-3 (PINNED for 38.4 ms)
├─ Blocking Reason: SYNCHRONIZED_MONITOR
├─ Monitored Object: com.example.inventory.WarehouseService@2b71e49
├─ Call Site: at com.example.inventory.WarehouseService.checkStock(WarehouseService.java:64)
│ at com.example.inventory.OrderController.submit(OrderController.java:29)
└─ Prescription: Refactor 'synchronized' block holding socket read to java.util.concurrent.locks.ReentrantLock.
{
"status": "DEGRADED",
"carrierPool": {
"parallelism": 16,
"activeCarriers": 16,
"pinnedCarriers": 6,
"utilization": "100.0%"
},
"diagnostics": {
"totalPinningEvents": 28,
"primaryBottleneck": "com.example.inventory.WarehouseService#checkStock",
"hikariPoolStatus": "PRESSURE_WARNING (Active: 10/10, Pending Virtual Threads: 412)",
"recommendation": "CRITICAL: Carrier pool saturation. 6 carriers pinned while 412 virtual threads wait for DB connections."
}
}
- Real-Time Pinning Detection: Detects virtual threads pinned to carrier threads via synchronized monitors or native calls.
- Connection Pool Starvation Alarms: Proactively flags connection exhaustion before database deadlocks cascade across your services.
- Actionable Engineering Prescriptions: Generates precise diagnostic reports with stack trace evidence and remediation guidance (e.g., refactoring
synchronizedtoReentrantLock). - Production Actuator Endpoint: Live JSON telemetry accessible via
/actuator/loom-doctor.
<dependency>
<groupId>io.github.frodygr</groupId>
<artifactId>loomdoctor-core</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>io.github.frodygr</groupId>
<artifactId>loomdoctor-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
LoomDoctor doctor = new LoomDoctor();
// Subscribe to pinning alerts
doctor.getPinningDetector().addListener(event -> {
System.err.printf("[PINNING ALERT] %s on carrier %s (%d ms)%n",
event.threadName(), event.carrierThreadName(), event.durationNanos() / 1_000_000);
});
// Run diagnostics
LoomDiagnosticsReport report = doctor.diagnose();
System.out.println("Health: " + report.status()); // HEALTHY, DEGRADED, or CRITICAL
report.recommendations().forEach(System.out::println);
Enable the endpoint in application.yml:
management:
endpoints:
web:
exposure:
include: "health,info,loom-doctor"
Access real-time diagnostics at:
GET http://localhost:8080/actuator/loom-doctor
{
"status": "DEGRADED",
"totalPinningEvents": 4,
"recentPinningSamples": [
{
"threadName": "virtual-worker-12",
"carrierThreadName": "ForkJoinPool-1-worker-3",
"eventType": "SYNCHRONIZED_BLOCK",
"durationNanos": 18240000
}
],
"recommendations": [
"Pinning Alert: 4 pinning events recorded. Replace synchronized blocks holding I/O with ReentrantLock."
]
}
Full architectural deep-dives, JFR integration guides, and API specifications are available in the Official Wiki.
Licensed under the Apache License, Version 2.0.
