jersey/jersey — pull request #38 by raphw (2013-11-07 22:02:03+00:00)
Title: JERSEY-2205: Jersey client - The ClientAsyncExecutorsFactory hosts thread pools that are not shut down when the hosting client is shut down.

When a JerseyClient that was using asynchronous requests in the past is Client#close()}}ed shortly before the exit point of a Java program, this program will not exit for a couple of seconds (unless {{System.exit(int) is called explicitly).

When looking at the thread dump of the application, I found that there were several alive threads named jersey-client-async-executor-x which were in a waiting state even after I shut the client down. Tracing this, I found that the ClientAsyncExecutorsFactory is hosting two ExecutorServices where the requestingExecutor represents an actual thread pool. This thread pool is not shut down explicitly, even though the hosting ClientRuntime was closed. I created a small patch that closes the underlying ExecutorService and my application did not longer exit with a minor delay.

I therefore suggest to add a method ExecutorsFactory#shutdown() which can be invoked by the ClientRuntime when the latter is closed in order to avoid this transitory memory leak. I suggest making this method a part of the ExecutorsFactory's contract for consistency reasons even though it is only required for the ClientRuntime since the ServerRuntime is not explicitly shut down.