<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>I encounter strange behavior of java.net.http.HttpClient in my application running in amazoncorretto:17.0.13, after some time instead of just one HttpClient-1-SelectorManager thread I'm starting observing dozens of such threads.<br><br>I'm creating only one instance of HttpClient in whole application, and confirming with logging that following snippet called only once:<br><br></div><pre style="border-top-width:1px;border-top-style:solid;border-top-color:#ccc;border-right-width:1px;border-right-style:solid;border-right-color:#ccc;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#ccc;border-left-width:1px;border-left-style:solid;border-left-color:#ccc;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:#f6f6f6;background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;margin-top:7px;margin-right:0px;margin-bottom:7px;margin-left:0px;padding-top:7px;padding-right:10px;padding-bottom:7px;padding-left:10px;text-wrap-mode:wrap;white-space-collapse:preserve;overflow-wrap:break-word;"><div><span class="color" style="color:rgb(207, 142, 109);">val </span>httpClient = HttpClient.newBuilder()<br style="max-width:100%;height:auto;"></div><div> .version(HttpClient.Version.<span class="color" style="color:rgb(199, 125, 187);"><i>HTTP_2</i></span>)<br style="max-width:100%;height:auto;"></div><div> .followRedirects(HttpClient.Redirect.<span class="color" style="color:rgb(199, 125, 187);"><i>NEVER</i></span>)<br style="max-width:100%;height:auto;"></div><div> .connectTimeout(Duration.ofSeconds(<span class="color" style="color:rgb(42, 172, 184);">60</span>))<br style="max-width:100%;height:auto;"></div><div> .build()<br style="max-width:100%;height:auto;"></div></pre><div><br>I also verified that no dependencies using HttpClient under the hood.<br><br></div><pre style="border-top-width:1px;border-top-style:solid;border-top-color:#ccc;border-right-width:1px;border-right-style:solid;border-right-color:#ccc;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#ccc;border-left-width:1px;border-left-style:solid;border-left-color:#ccc;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:#f6f6f6;background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;margin-top:7px;margin-right:0px;margin-bottom:7px;margin-left:0px;padding-top:7px;padding-right:10px;padding-bottom:7px;padding-left:10px;text-wrap-mode:wrap;white-space-collapse:preserve;overflow-wrap:break-word;">"HttpClient-18-SelectorManager" #20791 daemon prio=5 os_prio=0 cpu=31.77ms elapsed=3679.62s allocated=178K defined_classes=0 tid=0x00007f212c5dfce0 nid=0x541f runnable [0x00007f21993ee000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPoll.wait(java.base@17.0.13/Native Method)
at sun.nio.ch.EPollSelectorImpl.doSelect(java.base@17.0.13/EPollSelectorImpl.java:118)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(java.base@17.0.13/SelectorImpl.java:129)
- locked <0x000000008c488e38> (a sun.nio.ch.Util$2)
- locked <0x000000008c488de8> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(java.base@17.0.13/SelectorImpl.java:141)
at jdk.internal.net.http.HttpClientImpl$SelectorManager.run(java.net.http@17.0.13/HttpClientImpl.java:894)
Locked ownable synchronizers:
- None<br style="max-width:100%;height:auto;"></pre><div><br><br>So now I'm in situation when I see a lot (a few times they grow to 150+ threads) of such threads created over time and stay alive. I believe it's also hurting application performance which lead even to timeouts on http stack (but this is unconfirmed).<br><br>Any hint to understand why this might happen and how to debug root cause would be appreciated.<br> <br></div><div id="sig116664320"><div class="signature">--<br></div><div class="signature">Best regards,<br></div><div class="signature">Ruslan Ibragimov<br></div><div class="signature"><a href="https://heapy.io/">https://heapy.io/</a><br></div></div><div><br></div></body></html>