R442 · Infrastructure for agents at scale › Production incidents and cascading failure

The unmounting guarantee and its unlisted exceptions: a carrier thread that never comes back

Read the original

javavirtual-threadsjvmconcurrencyreliability

Source: Dr Heinz M. Kabutz, "Fun Java Puzzler with Virtual Threads and Hidden Classes", Java Specialists newsletter issue 339, 30 November 2026. Page header: "Java Version: 25+".

The puzzle

A nested class holds a static initializer that starts a virtual thread and nothing else:

static class WelcomeScript {
    static {
        Thread.startVirtualThread(() -> System.out.println("Hello world"));
    }
}
// main: new WelcomeScript();

Nothing prints. That part is unremarkable — virtual threads run on daemon carrier threads, main ends, the JVM exits before the message lands.

The second version adds a join inside the initializer, which is the obvious fix:

static {
    var hello = Thread.startVirtualThread(() -> System.out.println("Hello world"));
    hello.join();
}

Now, in the author's words: "Output is: Still nothing! Except this time, the program also does not terminate."

Why

It is a class-initialization deadlock, and the author names it as the classic case: "A thread is blocked from accessing any static elements of a class until <clinit> … has completed."

The lambda needs System.out. Reaching System.out from inside the lambda requires the enclosing class to be initialized. But the enclosing class's <clinit> is the thing waiting — main is parked in Thread.join inside it. A thread dump taken on Java 25.0.4.1 LTS shows the virtual thread "waiting on the Class initialization monitor" for the nested class while main waits for the virtual thread.

Mutual waiting on an initialization monitor is an old hazard and would be unremarkable on platform threads: one thread hangs, and the pool loses one worker.

What makes it a virtual-thread problem

The reason to use virtual threads is an invariant: when a virtual thread blocks, it unmounts from its carrier, and the carrier goes back to work. That invariant is what lets a small carrier pool serve a very large number of concurrent tasks, and it is the entire basis for sizing the pool by core count rather than by concurrency.

Here it does not hold. The author's description is "one carrier thread permanently blocked with that class initialization deadlock in Java 25" — the virtual thread stays mounted, and the carrier is gone for the lifetime of the process. Scale that deliberately and "We could jam up all the carrier threads": the demonstration queries getCarrierParallelism() through VirtualThreadSchedulerMXBean and arranges one such class per carrier thread.

So the failure is not one lost task. It is the loss of the scheduler's capacity to run any task, from a defect in one class's initializer.

This specific case is fixed: JDK-8369238, in Java 26, where "virtual threads get unmounted cleanly and so the output appears."

The part that is still open, and the reason it matters

The newsletter's second construction uses hidden classes — "Since Java 15, we can create 'hidden classes'. These are used internally to create helper classes, such as lambda implementation classes" — created through MethodHandles.lookup().defineHiddenClass(bytes, false) followed by ensureInitialized. The same byte[] can be defined repeatedly in one class loader without distinct names, which is what makes mass-producing the deadlock easy.

On that path, virtual threads are "not unmounted, not even in the latest versions of Java". The author logged it as JDK-8398565 and adds: "I don't think this is high priority, so not sure if/when it will be fixed."

Two features of that are worth separating from the puzzle.

First, hidden classes are not an exotic API you opt into. The quoted description says they back lambda implementation classes. Code that never calls defineHiddenClass still runs on them.

Second, and this is the generalisable point: the unmounting guarantee has an exception list, and the exception list is not published anywhere. Pinning conditions are documented. Class-initialization blocking was not on anyone's list until it was found, fixed for one path, and left open on another. A capacity model that assumes carriers are always reclaimed is resting on a set whose membership is discovered by incident.

The honest caveats, which are the author's own

This is a puzzler, and the newsletter says so. "I doubt this is ever a real-world problem." On the fix not reaching the current long-term release: "Back porting the fix to Java 25 seems like a huge amount of work, so will probably never be done."

That first caveat is correct about the contrived demonstration and understates the shape. Starting a thread from a static initializer is rare; libraries that start background work during initialization are not, and initialization order in a large dependency graph is not something an application author controls. The mechanism needs no deliberate join: it needs an initializer whose completion depends, by any path, on work that itself touches the class being initialized.

Tradeoffs

A guarantee that holds "almost always" is a different engineering object from one that holds. Carrier-pool sizing is cheap precisely because unmounting is assumed total. Each discovered exception converts a capacity argument into an empirical claim, and the cost is not the one lost thread — it is that the sizing rule no longer has a proof behind it.

Fixes land on the next release, not the supported one. The remedy here is Java 26 while Java 25 is the long-term release, and the author's advice is organisational rather than technical: "My policy is to always be on the latest release of the Oracle OpenJDK", and teams should "set up their CI/CD pipeline to make it feasible to upgrade quickly and easily." No code-level workaround is offered for either case. Upgrade latency is the mitigation.

Low-priority correctness bugs stay open on a reasonable judgement. A defect that needs a contrived program to trigger is correctly deprioritised. The side effect is that the exception list grows an entry nobody will read.

Failure modes

Sizing a carrier pool on the assumption that blocking always releases the carrier. It does on every documented blocking operation and did not here. The number to monitor is available carrier parallelism over time, not thread counts.

Starting threads — or anything that joins on a thread — from a static initializer. The deadlock requires nothing more than that the initializer's completion depend on work that touches the class under initialization.

Diagnosing carrier starvation as load. A jammed carrier pool looks like saturation. The distinguishing evidence is in the thread dump: a thread waiting on a class initialization monitor is not a busy thread.

Assuming hidden classes are opt-in. They underpin lambda implementation classes, so the still-unfixed path is reachable without naming the API.

Reading "fixed" without reading "fixed where". JDK-8369238 is fixed in Java 26 and will probably never be backported to 25.

Treating a puzzler as entertainment only. The construction is artificial; the invariant it breaks is one that production capacity planning depends on.

Review checklist

For any system relying on virtual threads or a similar mount/unmount scheduler:

Connections

See R442 on the knowledge map

Part of the Research notes on nowucca.com. Figures and claims are the source's own unless the summary says otherwise.