1

I'm using the Java's new (since version 11) HttpClient within a Servlet based web application:

private static final HttpClient HTTP_CLIENT =
  .connectTimeout(Duration.ofSeconds(5))
  .followRedirects(HttpClient.Redirect.NORMAL)
  .build();

...

public void httpPostAsyncToEndpoint(WebEndpoint endpoint, Map<String,String> params) {
  HttpRequest req = buildRequest(endpoint, params);
  CompletableFuture<HttpResponse<String>> future = HTTP_CLIENT.sendAsync(req, HttpResponse.BodyHandlers.ofString());
  future.thenAccept((HttpResponse<String> res) -> {
    if (res.statusCode() >= 400) {
      if (LOGGER.isErrorEnabled()) {
        LOGGER.error("{} HTTP response returned from endpoint {}", endpoint, res.statusCode());
      }
    }
  }).exceptionally(ex -> {
    if (LOGGER.isErrorEnabled()) {
      LOGGER.error("Could not audit event using endpoint {}", endpoint, ex);
    }
    return null;
  });
}

Everything is working great, except that when the web application is restarted on Tomcat the following warning is produced:

14-Aug-2020 09:21:16.996 WARNING [http-nio-8080-exec-18] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MyApp] appears to have started a thread named [HttpClient-3-SelectorManager] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base/sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
 java.base/sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:357)
 java.base/sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:182)
 java.base/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:124)
 java.base/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:136)
 java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.run(HttpClientImpl.java:867)

How can I prevent this? I've attempted to use a custom ThreadFactory which returns only daemon threads:

HttpClient.newBuilder()
  .executor(Executors.newSingleThreadExecutor((Runnable r) -> {
    Thread t = new Thread(r);
    t.setDaemon(true);
    return t;
  }))
  .connectTimeout(Duration.ofSeconds(5))
  .followRedirects(HttpClient.Redirect.NORMAL).build();

but the warning persists.

I'm using OpenJDK 11.0.7 on Tomcat 9.

Brice Roncace
  • 10,110
  • 9
  • 60
  • 69

1 Answers1

3

The selector manager thread will remain alive as long as the reference to the HttpClient is alive - or as long as an operation initiated by the client is still in progress. It may take a couple of seconds for the thread to detect that the HttpClient is no longer referenced. So I don't believe that what you have is an actual leak - unless the class that held the static reference to the HttpClient stays pinned in memory for other reasons.

daniel
  • 2,665
  • 1
  • 8
  • 18