From b66c278847f2a45fb6022081166a57f009a78ba9 Mon Sep 17 00:00:00 2001 From: Tomer Shafir <86107951+tomershafir@users.noreply.github.com> Date: Tue, 6 Feb 2024 05:01:54 +0200 Subject: [PATCH] periodic-sched: fix file leak and reuse a single file (#142) --- agent/src/main/java/io/pyroscope/javaagent/Profiler.java | 8 +++----- .../javaagent/impl/SamplingProfilingScheduler.java | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/agent/src/main/java/io/pyroscope/javaagent/Profiler.java b/agent/src/main/java/io/pyroscope/javaagent/Profiler.java index a6cbf00..4cd1a34 100644 --- a/agent/src/main/java/io/pyroscope/javaagent/Profiler.java +++ b/agent/src/main/java/io/pyroscope/javaagent/Profiler.java @@ -27,10 +27,10 @@ public final class Profiler { private final AsyncProfiler instance = PyroscopeAsyncProfiler.getAsyncProfiler(); Profiler(Config config) { - setConfig(config); + reset(config); } - public void setConfig(final Config config) { + public void reset(final Config config) { this.config = config; this.alloc = config.profilingAlloc; this.lock = config.profilingLock; @@ -38,7 +38,7 @@ public void setConfig(final Config config) { this.interval = config.profilingInterval; this.format = config.format; - if (format == Format.JFR) { + if (format == Format.JFR && null == tempJFRFile) { try { // flight recorder is built on top of a file descriptor, so we need a file. tempJFRFile = File.createTempFile("pyroscope", ".jfr"); @@ -46,8 +46,6 @@ public void setConfig(final Config config) { } catch (IOException e) { throw new IllegalStateException(e); } - } else { - tempJFRFile = null; } } diff --git a/agent/src/main/java/io/pyroscope/javaagent/impl/SamplingProfilingScheduler.java b/agent/src/main/java/io/pyroscope/javaagent/impl/SamplingProfilingScheduler.java index b8f9830..0243128 100644 --- a/agent/src/main/java/io/pyroscope/javaagent/impl/SamplingProfilingScheduler.java +++ b/agent/src/main/java/io/pyroscope/javaagent/impl/SamplingProfilingScheduler.java @@ -55,7 +55,7 @@ public void start(Profiler profiler) { final EventType t = config.samplingEventOrder.get(i); final Config tmp = isolate(t, config); logger.log(Logger.Level.DEBUG, "Config for %s ordinal %d: %s", t.id, i, tmp); - profiler.setConfig(tmp); + profiler.reset(tmp); dumpProfile(profiler, samplingDurationMillis, uploadInterval); } } :