Devculi Blog

Java 11 HTTPClient Timeout Issue

Posted in: Uncategorized

Symptom

A record is locked in the database and we receive a slow query notification from the database management team

SQL:

UPDATE product SET price = 1000 WHERE id = 1000

The query has almost nothing to optimize, but operations cannot be performed on this record

Problem

Thread dump trên server look like this:

"pool-3-thread-2" #57 prio=5 os_prio=0 cpu=7204.26ms elapsed=16809.99s tid=0x000056033ed08800 nid=0x59 waiting on condition  [0x00007f263fc89000]
   java.lang.Thread.State: WAITING (parking)
    at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
    - parking to wait for  <0x00000000c54cb950> (a java.util.concurrent.CompletableFuture$Signaller)
    at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
    at java.util.concurrent.CompletableFuture$Signaller.block([email protected]/CompletableFuture.java:1796)
    at java.util.concurrent.ForkJoinPool.managedBlock([email protected]/ForkJoinPool.java:3128)
    at java.util.concurrent.CompletableFuture.waitingGet([email protected]/CompletableFuture.java:1823)
    at java.util.concurrent.CompletableFuture.get([email protected]/CompletableFuture.java:1998)
    at jdk.internal.net.http.HttpClientImpl.send([email protected]/HttpClientImpl.java:541)
    at jdk.internal.net.http.HttpClientFacade.send([email protected]/HttpClientFacade.java:119)

CompletableFuture.get() is called without timeout while Openjdk’s HttpClient having a bug with timeout

During this request flow, the record will be locked for updates and then a notification email will be sent to the user. The email sending step calls OpenJDK HTTPClient and it has blocked the request processing flow from the user of the embedded tomcat server -> request stuck

Solution

Use async for threaded mail delivery with timeout

Refs