Skip to content

Commit d514569

Browse files
authored
Ensure that profile is shutdown (#2411)
1 parent e08a203 commit d514569

File tree

1 file changed

+13
-10
lines changed
  • agent/agent-profiler/agent-service-profiler/src/main/java/com/microsoft/applicationinsights/serviceprofilerapi/profiler

1 file changed

+13
-10
lines changed

agent/agent-profiler/agent-service-profiler/src/main/java/com/microsoft/applicationinsights/serviceprofilerapi/profiler/JfrProfiler.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.lang.management.ManagementFactory;
4444
import java.time.Duration;
4545
import java.time.Instant;
46+
import java.time.temporal.ChronoUnit;
4647
import java.util.UUID;
4748
import java.util.concurrent.CompletableFuture;
4849
import java.util.concurrent.ScheduledExecutorService;
@@ -179,7 +180,12 @@ protected Recording startRecording(AlertMetricType alertType, Duration duration)
179180
try {
180181
activeRecordingFile = createJfrFile(duration);
181182

182-
RecordingOptions recordingOptions = recordingOptionsBuilder.build();
183+
// As a fallback in case recording closing logic does not succeed, set the recording
184+
// duration to the expected duration plus 60 seconds
185+
Duration requestedDuration = duration.plus(60, ChronoUnit.SECONDS);
186+
187+
RecordingOptions recordingOptions =
188+
recordingOptionsBuilder.duration(requestedDuration.toMillis() + " ms").build();
183189

184190
this.activeRecording = createRecording(recordingOptions, recordingConfiguration);
185191

@@ -273,15 +279,12 @@ private static void closeRecording(Recording recording, File recordingFile) {
273279
LOGGER.error("Failed to close recording", e);
274280
} catch (JfrStreamingException e) {
275281
LOGGER.error("Internal JFR Error", e);
276-
} finally {
277-
try {
278-
recording.stop();
279-
recording.close();
280-
} catch (IOException e) {
281-
LOGGER.error("Failed to close recording", e);
282-
} catch (JfrStreamingException e) {
283-
LOGGER.error("Internal JFR Error", e);
284-
}
282+
}
283+
} finally {
284+
try {
285+
recording.close();
286+
} catch (IOException e) {
287+
LOGGER.error("Failed to close recording", e);
285288
}
286289
}
287290
}

0 commit comments

Comments
 (0)