Correct way to shutdown smack

I’m developing application that deployed on Tomcat. I’m using Smack to implement CCS FCM over XMPP. I’m monitoring shutdown hook and on shutdown runs connection.disconnect(). However on undeploy I have got this trace in catalina logs.

10-Oct-2017 17:44:06.952 WARNING [https-jsse-nio2-8081-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [wfmserver] appears to have started a thread named [Smack-Ping 0 (0)] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 10-Oct-2017 17:44:06.952 WARNING [https-jsse-nio2-8081-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [wfmserver] appears to have started a thread named [Smack-Incoming Processor 0 (0)] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:403) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 10-Oct-2017 17:44:06.953 WARNING [https-jsse-nio2-8081-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [wfmserver] appears to have started a thread named [Smack-Single Threaded Executor 0 (0)] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 10-Oct-2017 17:44:06.953 WARNING [https-jsse-nio2-8081-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [wfmserver] appears to have started a thread named [MappedByteBufferSource Async Flush Thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1081) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 10-Oct-2017 17:44:06.953 WARNING [https-jsse-nio2-8081-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [wfmserver] appears to have started a thread named [Smack-Cached Executor 0 (0)] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460) java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362) java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748)

So, my question is how I should shutdown smack? How I can prevent memory leaks?

@Flow need your attention here.

Guys, please, need your help.

I didn’t have a close look at the code, but it’s very possible that there are some threads around after the last Smack connection called disconnect(). That is how it is currently designed, and while not ideal, it works for most cases and changing it would require a significant effort (Hint: I am available for freelance work ;)).

Related side note: The #smack IRC channel on freenode is ideal to discuss those things too.