As noted by the reporter in the comments of 
https://bugs.openjdk.java.net/browse/JDK-7146776, the `URLStreamHandler` in its 
`getHostAddress` is incorrectly synchronizing on the `URLStreamHandler` 
instance, instead of synchronizing on the `URL` instance that is being passed 
to that method.

The implementation of `URLStreamHandler#getHostAddress` checks for the (package 
private) `hostAddress` member of the passed `URL` and if it isn't set then does 
a DNS lookup to get the address and finally sets the `hostAddress` member of 
the `URL` to this returned address. The access to `hostAddress` of `URL` needs 
to be `synchronized` since it can be updated (in a different thread) in the 
`URL#set` methods.

The commit here removes the synchronization from the 
`URLStreamHandler#getHostAddress` and moves the entire implementation into a 
new (package private) `URL#getHostAddress` method and then calls this new 
method from the `URLStreamHandler#getHostAddress`. I decided to do it this way 
instead of just leaving the implementation in `URLStreamHandler#getHostAddress` 
and synchronizing on the passed `URL` instance, since IMO, this makes it much 
more easier to see (in one place within the `URL` class) what the 
synchronization requirements are when dealing with the `hostAddress`. 
Furthermore, this now allows the `hostAddress` to be made a `private` member 
(which I have done in this commit) instead of the previous package private 
access. I checked that no other code tries to access this package private 
`URL#hostAddress`.

I can't think of an easy way to add a jtreg test for this change, so I haven't 
added one.

-------------

Commit messages:
 - 7146776: deadlock between URLStreamHandler.getHostAddress and 
file.Handler.openconnection

Changes: https://git.openjdk.java.net/jdk/pull/2109/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2109&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-7146776
  Stats: 43 lines in 2 files changed: 23 ins; 15 del; 5 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2109.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2109/head:pull/2109

PR: https://git.openjdk.java.net/jdk/pull/2109

Reply via email to