diff --git a/core/src/main/java/com/uber/m3/tally/ScopeBuilder.java b/core/src/main/java/com/uber/m3/tally/ScopeBuilder.java index 09a00ff..363b64b 100644 --- a/core/src/main/java/com/uber/m3/tally/ScopeBuilder.java +++ b/core/src/main/java/com/uber/m3/tally/ScopeBuilder.java @@ -153,6 +153,11 @@ public Scope reportEvery(Duration interval) { public Scope reportEvery(Duration interval, Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { if (interval.compareTo(Duration.ZERO) <= 0) { + if (reporter != null) { + // The underlying transport in the reporter may have been opened at this point, so make sure to close + // that to not leak resources. + reporter.close(); + } throw new IllegalArgumentException("Reporting interval must be a positive Duration"); } diff --git a/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java b/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java index 923d0cd..84417ce 100644 --- a/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java +++ b/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java @@ -153,6 +153,9 @@ private M3Reporter(Builder builder) { addAndRunProcessor(builder.metricTagSet); } catch (TTransportException | SocketException e) { + if (transport != null) { + transport.close(); + } throw new RuntimeException("Exception creating M3Reporter", e); } }