diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 2c3f2ace2..ddcc5445d 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -776,29 +776,32 @@ public void cleanUp() { this.appConfig = null; if (connection != null) { connection.closeConnector(); + connection = null; } } /** * Asynchronoush cleanup of instance, including connection. * */ - public void cleanUpAsync() { + public synchronized void cleanUpAsync() { + appConfig = null; + class AsyncCleaner implements Runnable { - Instance instance; + Connection conn; - AsyncCleaner(Instance instance) { - this.instance = instance; + AsyncCleaner(Connection conn) { + this.conn = conn; } @Override public void run() { - instance.appConfig = null; - if (instance.connection != null) { - instance.connection.closeConnector(); - } + conn.closeConnector(); } } - new Thread(new AsyncCleaner(this)).start(); + if (connection != null) { + new Thread(new AsyncCleaner(connection), "jmx-closer").start(); + connection = null; + } } }