changeset 56887:d4cae08239f7

8230767: FlightRecorderListener returns null recording Reviewed-by: mseledtsov, mgronlun
author egahlin
date Mon, 11 Nov 2019 14:47:37 +0100
parents 0c5fb962e11e
children e4d7fcab43d7
files src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java test/jdk/jdk/jfr/api/recorder/TestRecorderListenerWithDump.java
diffstat 2 files changed, 41 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java	Mon Nov 11 05:09:31 2019 -0800
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java	Mon Nov 11 14:47:37 2019 +0100
@@ -484,7 +484,10 @@
         }
         for (FlightRecorderListener cl : PlatformRecorder.getListeners()) {
             try {
-                cl.recordingStateChanged(getRecording());
+                // Skip internal recordings
+                if (recording != null) {
+                    cl.recordingStateChanged(recording);
+                }
             } catch (RuntimeException re) {
                 Logger.log(JFR, WARN, "Error notifying recorder listener:" + re.getMessage());
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jdk/jfr/api/recorder/TestRecorderListenerWithDump.java	Mon Nov 11 14:47:37 2019 +0100
@@ -0,0 +1,37 @@
+package jdk.jfr.api.recorder;
+
+import java.nio.file.Paths;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import jdk.jfr.FlightRecorder;
+import jdk.jfr.FlightRecorderListener;
+import jdk.jfr.Recording;
+/**
+ * @test TestRecorderListenerWithDump
+ *
+ * @key jfr
+ * @requires vm.hasJFR
+ * @run main/othervm jdk.jfr.api.recorder.TestRecorderListenerWithDump
+ */
+public class TestRecorderListenerWithDump {
+
+    public static void main(String... args) throws Exception {
+        AtomicBoolean nullRecording = new AtomicBoolean();
+        FlightRecorder.addListener(new FlightRecorderListener() {
+            public void recordingStateChanged(Recording r) {
+                if (r == null) {
+                    nullRecording.set(true);
+                } else {
+                    System.out.println("Recording " + r.getName() + " " + r.getState());
+                }
+            }
+        });
+        try (Recording r = new Recording()) {
+            r.start();
+            r.dump(Paths.get("dump.jfr"));
+        }
+        if (nullRecording.get()) {
+            throw new Exception("FlightRecorderListener returned null recording");
+        }
+    }
+}