JFR request events: every request fails without jdk.jfr, and the first request initializes JFR
Expected Behavior
- A Netty HTTP server on a runtime without the
jdk.jfrmodule (jlink image,--limit-modules) serves requests and emits no JFR events. - While no Flight Recorder exists, serving a request does not initialize JFR. Micronaut's HTTP and Loom events are
@Enabled(false)anyway (HttpRequestEvent.java#L33-L36).
Actual Behaviour
1. Bug: without jdk.jfr, every request fails. NativeImageUtils.JFR_AVAILABLE is !inImageCode() (NativeImageUtils.java#L52-L56), always true on the JVM, so OutboundAccessImpl calls Http1RequestEvent.isTurnedOn() (PipeliningServerHandler.java#L987-L995):
Micronaut Server Error - No request state present. Cause: jdk/jfr/Event
java.lang.NoClassDefFoundError: jdk/jfr/Event
at io.micronaut.http.server.netty.handler.PipeliningServerHandler$OutboundAccessImpl.<init>(PipeliningServerHandler.java:923)The client gets no response or a 500. MultiplexedStream and LoomCarrierGroup$Runner use the same guard. The events came in 4.9.0 (#11800); 4.10.x, 5.2.x and 5.3.x have the same guard. Netty's events survive this: it catches the failure of FlightRecorder.isAvailable() (PlatformDependent.java#L268-L277).
2. Startup: the first request initializes JFR although nothing is recording. isTurnedOn() reads a static event instance (Http1RequestEvent.java#L29-L34), initializing the class. HotSpot injects FlightRecorder.register into the <clinit> of every non-abstract jdk.jfr.Event subclass not annotated @Registered(false) (jfrEventClassTransformer.cpp#L260-L279), and the first registration builds JFR's MetadataRepository: 114 jdk.jfr classes and 56 internal NoSuchMethodExceptions on the event loop, a 21–23 ms window without a cache and about 16 ms with the JDK AOT cache (3 logged runs each).
Netty's allocator events do the same on the first allocation. -XX:LogClassLoadingCauseFor=jdk.jfr.internal.MetadataRepository -Xlog:class+load+cause* shows Netty's AllocateChunkEvent as the trigger by default and Http1RequestEvent with -Dio.netty.jfr.enabled=false. Gating one library only moves the trigger (Netty-only gate: −2.5 ms [−6.0, +1.9]). The Netty half is netty/netty#17612.
Steps To Reproduce
- Build a Micronaut HTTP server as a fat JAR (for example the application below).
java --limit-modules java.se,jdk.unsupported,jdk.zipfs -jar app.jar, request/hello:NoClassDefFoundError: jdk/jfr/Event, no 200.java -Xlog:class+load -jar app.jar, one request, no recording:jdk.jfr.internal.MetadataRepositoryis loaded.
Environment Information
macOS 26.6.2 (Apple Silicon, 12 CPUs), Homebrew OpenJDK 25.0.4.1, Netty 4.2.18.Final
Example Application
Version
5.1.15 (reproduced); code unchanged on 5.2.x and 5.3.x.
How it was found and measured
Found while profiling startup of micronaut-runner on the sample above (analysis: https://github.com/alvarosanchez/micronaut-runner/issues/153). Both gates (this issue and the Netty one) were applied as prototype class patches with the sketch's logic; the sketch itself compiles but was not timed. Randomised interleaved paired rounds (3 warm-up, then n measured), readiness = spawn to first HTTP 200 on /hello, median paired difference with bootstrap 95% CI:
| n | No cache | JDK AOT cache | |
|---|---|---|---|
| Shadow JAR | 25 | −29.6 ms [−36.2, −20.1] (533.9 → 506.4) | −21.6 ms [−27.5, −12.1] (240.2 → 221.4) |
| Runner JAR (4 sessions) | 75 | −24.4 ms [−27.6, −22.1] | −17.4 ms [−18.9, −16.2] |
One diagnostic launch each: classes loaded before the first 200 went 6,208 → 6,069 (jdk.jfr 114 → 1), and the trained AOT cache shrank by 770,048 bytes. With Http1RequestEvent enabled, a launch-time -XX:StartFlightRecording recorded 6 events and a later jcmd JFR.start 5, with and without the gates.
Proposed change
- core (bug fix):
JFR_AVAILABLE = !inImageCode() && ModuleLayer.boot().findModule("jdk.jfr").isPresent(). - http-netty (startup): an
@Internalhelper returningJFR_AVAILABLE && FlightRecorder.isInitialized(), cachingtrue(the JDK flag never reverts), used at the four sites. Completion is already null-safe (PipeliningServerHandler.java#L1232-L1247, MultiplexedServerHandler.java#L496-L509). Not incore:jdeps --print-module-depson 5.2.2 givesjava.basefor micronaut-core butjava.base,jdk.jfrfor http-netty and http-server-netty, and CLI or function apps should not gainjdk.jfr. - Once Netty ships its half, bump
managed-netty(libs.versions.toml#L94). Until then-Dio.netty.jfr.enabled=falsegives nearly all of the gain.
No event is lost: initialized is set only in getFlightRecorder() (FlightRecorder.java#L155-L168), which every way of creating a recording calls first: new Recording(), RecordingStream, -XX:StartFlightRecording, jcmd JFR.start and the MXBean.
Alternative considered: @Registered(false) plus a FlightRecorderListener that registers the events keeps the check foldable, but registers and loads classes inside synchronized (PlatformRecorder.class). Not worth it for a per-request check.
Compatibility: events are identical for any recording, except for a request already in flight when the first recorder appears. Until then the check costs a plain read plus a volatile read per request (and per Loom loop tick): tiny, but not a saving, since today's check may fold away in C2. Native image is unchanged. Fits a 5.2.x patch, merged forward.
Sketch--- a/core/src/main/java/io/micronaut/core/util/NativeImageUtils.java
- public static final boolean JFR_AVAILABLE = !inImageCode();
+ public static final boolean JFR_AVAILABLE = !inImageCode()
+ && ModuleLayer.boot().findModule("jdk.jfr").isPresent();
--- /dev/null
+++ b/http-netty/src/main/java/io/micronaut/http/netty/JfrSupport.java
+@Internal
+public final class JfrSupport {
+ private static boolean recorderSeen; // plain: a stale read costs one more call
+
+ private JfrSupport() {
+ }
+
+ /** @return whether JFR events may be recorded now; never initializes an event class */
+ public static boolean isRecorderInitialized() {
+ if (!NativeImageUtils.JFR_AVAILABLE) {
+ return false;
+ }
+ if (recorderSeen) {
+ return true;
+ }
+ if (jdk.jfr.FlightRecorder.isInitialized()) {
+ recorderSeen = true;
+ return true;
+ }
+ return false;
+ }
+}
--- a/http-server-netty/.../handler/PipeliningServerHandler.java (L989)
- if (NativeImageUtils.JFR_AVAILABLE && Http1RequestEvent.isTurnedOn()) {
+ if (JfrSupport.isRecorderInitialized() && Http1RequestEvent.isTurnedOn()) {
--- a/http-server-netty/.../handler/MultiplexedServerHandler.java (L112)
- if (NativeImageUtils.JFR_AVAILABLE && Http2RequestEvent.isTurnedOn()) {
+ if (JfrSupport.isRecorderInitialized() && Http2RequestEvent.isTurnedOn()) {
--- a/http-netty/.../channel/loom/LoomCarrierGroup.java (L465, L532)
- if (NativeImageUtils.JFR_AVAILABLE && ContinuationScheduled.INSTANCE.isEnabled()) {
+ if (JfrSupport.isRecorderInitialized() && ContinuationScheduled.INSTANCE.isEnabled()) {
- if (NativeImageUtils.JFR_AVAILABLE && LoopTick.INSTANCE.isEnabled()) {
+ if (JfrSupport.isRecorderInitialized() && LoopTick.INSTANCE.isEnabled()) {Tests
- Forked JVM, one HTTP/1.1 request under
-Xlog:class+load: nojdk.jfr.internal.MetadataRepository(with-Dio.netty.jfr.enabled=falseuntilmanaged-nettyhas the Netty fix). - A
RecordingStreamstarted after startup that enablesHttp1RequestEventstill receives it; likewise an HTTP/2 request (Http2RequestEvent) and a Loom-carrier run. --limit-modules java.se,jdk.unsupported,jdk.zipfs: the request returns 200.
Related: https://github.com/alvarosanchez/micronaut-runner/issues/147 attributes this first-request cost to the Micronaut and Netty versions until both fixes ship.
Source: micronaut-projects/micronaut-core