HADOOP-17052. NetUtils.connect() throws unchecked exception (UnresolvedAddressException) causing clients to abort (#2036)

Contributed by Dhiraj Hegde.

Signed-off-by: Mingliang Liu <liuml07@apache.org>
This commit is contained in:
Dhiraj 2020-06-01 10:49:17 -07:00 committed by GitHub
parent ae13a5ccbe
commit 9fe4c37c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -37,6 +37,7 @@ import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.net.ConnectException;
import java.nio.channels.SocketChannel;
import java.nio.channels.UnresolvedAddressException;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.*;
@ -534,6 +535,8 @@ public class NetUtils {
}
} catch (SocketTimeoutException ste) {
throw new ConnectTimeoutException(ste.getMessage());
} catch (UnresolvedAddressException uae) {
throw new UnknownHostException(uae.getMessage());
}
// There is a very rare case allowed by the TCP specification, such that

View File

@ -95,7 +95,25 @@ public class TestNetUtils {
assertInException(se, "Invalid argument");
}
}
@Test
public void testInvalidAddress() throws Throwable {
Configuration conf = new Configuration();
Socket socket = NetUtils.getDefaultSocketFactory(conf)
.createSocket();
socket.bind(new InetSocketAddress("127.0.0.1", 0));
try {
NetUtils.connect(socket,
new InetSocketAddress("invalid-test-host",
0), 20000);
socket.close();
fail("Should not have connected");
} catch (UnknownHostException uhe) {
LOG.info("Got exception: ", uhe);
}
}
@Test
public void testSocketReadTimeoutWithChannel() throws Exception {
doSocketReadTimeoutTest(true);