Fix 'initialRequest' guard might be incorrect in DohResolver and potential 'rollback' on 'lastRequest' among concurrent requests. #345
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi all, thanks for such solid & amazing dns implementation in Java world!
Trying to employ the DoH capability to my project, and found that there might be two hard-to-find issues lying in current DoH implementation.
Related code snippet:
dnsjava/src/main/java/org/xbill/DNS/DohResolver.java
Lines 478 to 483 in a6371af
Imaging that there is a service running DohResolver on a server booted recently, when DohResolver is about to send the first request, its
lastRequest.get() == 0L
,idleConnectionTimeout.get() == some user-defined value from constructor or 2mins by default
. IfidleConnectionTimeout
is larger than currentSystem.nanoTime()
, the value of boolean guardisInitialRequest
will be false, thing goes wrong.(Eg: someone constructs a DohResolver with unary constructor but its server was booted 1min before. On my Ubuntu 18.04 server with Azul Zulu 8.0.322 JVM,

System.nanoTime()
is equals to my server's uptime.)To address such case, we may need to introduce a new state variable to indicate the 'initial request' was acutally completed, to ensure the connection pool was created after the first connection was established successfully.
dnsjava/src/main/java/org/xbill/DNS/DohResolver.java
Lines 478 to 488 in e1e5c0c
dnsjava/src/main/java/org/xbill/DNS/DohResolver.java
Lines 541 to 557 in a6371af
If there are two concurrent DoH requests happend, one with a smaller 'startTime' got the response from server behind of another one with a larger 'startTime', then the value 'lastRequest' will first be set to the large one, then 'bump' to the small one.
We may also need to introduce a CAS get-and-set operation like code snippet below to ensure value in 'lastRequest' field is always monotonic.
dnsjava/src/main/java/org/xbill/DNS/DohResolver.java
Lines 540 to 552 in e1e5c0c