koszta5 commented on code in PR #721:
URL: 
https://github.com/apache/httpcomponents-client/pull/721#discussion_r2345176672


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractHttpAsyncClientBase.java:
##########
@@ -116,7 +116,7 @@ public final void close(final CloseMode closeMode) {
         }
         ioReactor.initiateShutdown();
         ioReactor.close(closeMode);
-        executorService.shutdownNow();
+        executorService.shutdown();

Review Comment:
   if you give me a little bit more time next week I can provide heap dump 
details / proof and possibly even a reproducer in a test
   But to put it real simple
   - in AbstractHttpAsyncClientBase constructor executor service is created as 
follows
   ```
   this.executorService = Executors.newSingleThreadExecutor(threadFactory);
   ```
   - the above leads to AutoShutdownDelegatedExecutorService (see 
implementation of newSingleThreadExecutor() )
   - see constructor of AutoShutdownDelegatedExecutorService (it creates a 
phantom reference via cleanable variable in this constructor)
   - see shutdown() method in AutoShutdownDelegatedExecutorService - it is 
overriden and runs super method but also (very importantly!) cleanable.clean() 
which removes the phantom reference
   - on the other hand if you just call shutdownNow() on 
AutoShutdownDelegatedExecutorService it does not run the cleaner and does not 
remove the phantom reference which is very important for GC. 
   
   Note that this problem happens at least in JDK21. Not sure about other 
releases. You could argue that its a JDK bug and it might - other way to look 
at it that its improper API usage.
   
   I had to fork our httpclient and I can confirm that this fix works and we 
are no longer leaking to metaspace. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to