diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrThreadLocal.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrThreadLocal.java index d5f58382182d..98d7daea7ce8 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrThreadLocal.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrThreadLocal.java @@ -123,7 +123,9 @@ public UnsignedWord getThreadLocalBufferSize() { } public void teardown() { + // At this point all native buffers should be freed already. getNativeBufferList().teardown(); + // At this point Java buffers will be retired or freed. getJavaBufferList().teardown(); } @@ -161,10 +163,12 @@ public static void stopRecording(IsolateThread isolateThread, boolean freeJavaBu flushToGlobalMemoryAndFreeBuffer(nb); JfrBuffer jb = javaBuffer.get(isolateThread); - javaBuffer.set(isolateThread, Word.nullPointer()); if (freeJavaBuffer) { + javaBuffer.set(isolateThread, Word.nullPointer()); flushToGlobalMemoryAndFreeBuffer(jb); } else { + // Do not reset the thread local since we may need it to reinstate the buffer in the + // next recording. flushToGlobalMemoryAndRetireBuffer(jb); } @@ -213,7 +217,7 @@ private static void flushToGlobalMemoryAndFreeBuffer(JfrBuffer buffer) { @Uninterruptible(reason = "Locking without transition requires that the whole critical section is uninterruptible.") private static void flushToGlobalMemoryAndRetireBuffer(JfrBuffer buffer) { assert VMOperation.isInProgressAtSafepoint(); - if (buffer.isNull()) { + if (buffer.isNull() || JfrBufferAccess.isRetired(buffer)) { return; }