Name: krT82822 Date: 07/29/99
In JDK 1.2.2, getHostByName() method of InetAddress class deadlocks.
This is a simple miscoding for thread synchronization but this is a very serious bug for the network programming. Please fix this bug quickly.
For example, one thread gets the lock of an addressCache instance (1) and tries to get the lock of InetAddressCachePolicy class (2) in order to get an IP address from an address cache.
The sun.net.InetAddressCachePolicy#get() method is a synchronized method.
private static Object getCachedAddress(String hostname) {
hostname = hostname.toLowerCase();
synchronized (addressCache) { // (1)
if (InetAddressCachePolicy.get() == 0) { // (2)
return null;
}
And another thread gets the lock of InetAddressCachePolicy class and tries to get the lock of an addressCache instance in order to register an IP address to an address cache.
private static void cacheAddress(String hostname, Object address) {
synchronized (InetAddressCachePolicy.class) { // (3)
// if the cache policy is to cache nothing, just return
if (InetAddressCachePolicy.get() == 0) {
return;
}
long expiration = -1;
if (InetAddressCachePolicy.get() != InetAddressCachePolicy.FOREVER) {
expiration = System.currentTimeMillis() +
(InetAddressCachePolicy.get() * 1000);
}
cacheAddress(hostname, address, expiration);
}
}
private static void cacheAddress(String hostname, Object address, long expiration) {
hostname = hostname.toLowerCase();
synchronized (addressCache) { // (4)
Then they are deadlocked.
-----------
7/29/99 kevin.ryan@eng -- submitting as-is
(Review ID: 88440)
======================================================================
Name: skT88420 Date: 09/10/99
I am running a DNS name resolver with between 1-30 threads performing concurrent name resolution. This code snip below runs in a class derived from thread. A stream of "hostAddr" addresses are fed to the thread and the results are placed into a table.
ia = InetAddress.getByName( hostAddr ) ;
hostName = ia.getHostName() ;
Sometimes, (1 in 3, or 1 in 4 cases ) the threads become deadlocked in java.net.InetAddress:src line 328
326: private static Object getCachedAddress(String hostname) {
327: hostname = hostname.toLowerCase();
328: synchronized (addressCache) {
329: if (InetAddressCachePolicy.get() == 0) {
330: return null;
331: }
The problem is more common as the thread count increases. It is quite easily produced with 40 threads. The lockup should occur within a few hundred dns requests. When I found the issue and began investigating the src file, I was horrified to find a cache that grows without bounds. Since I will be processing 100,000(s) of InetAddress in a Session this cache would have become a large issue.
(1) Fix the deadlock Issue
(2) make public the existance of the cache, and the sun.net.inetaddr.ttl property. The fact that you need to browse the java.net.InetAddress file to find the sun.net.InetAddressCachePolicy and then disassemble with javap to find the existance of the property is insufficient. It at least deserves a note in the JavaDoc for InetAddress.
(Review ID: 95113)
======================================================================