Skip to content

RuntimeMetrics JFR startup: RecordingStream.start() can run after close(), causing uncaught IllegalStateException #15229

@mlbiscoc

Description

@mlbiscoc

Describe the bug

In io.opentelemetry.instrumentation.runtimemetrics.java17 RuntimeMetrics starts JFR via RecordingStream on a separate daemon thread. If a caller creates RuntimeMetrics and then quickly calls close() the background thread may invoke RecordingStream.start() after the stream has already been closed. Has anyone seen this issue?

Steps to reproduce

I am able to reproduce it locally very simply by doing the following:

var otel = OpenTelemetrySdk.builder().build();
for (int i = 0; i < 50; i++) {
      var runtime = RuntimeMetrics.builder(otel)
          .enableAllFeatures()
          .build();
      runtime.close();
}

Expected behavior

There should probably be a solution to synchronously start/stop or expose a flag/hook to check that the JFR started before the user calls close.

Actual behavior

JFR throws:

java.lang.IllegalStateException: Recording can only be started once.

Javaagent or library instrumentation version

2.21.0-alpha

Environment

OpenJDK Runtime Environment Temurin-21.0.6+7
macOS: Sequoia 15.7.1

Additional context

Found this on 2.11.0-alpha but also still appeared when testing this with 2.21.0-alpha

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageNew issue that requires triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions