Skip to content

Commit 910d88e

Browse files
dhirajhliuml07
authored andcommitted
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>
1 parent 07b8963 commit 910d88e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.net.UnknownHostException;
3838
import java.net.ConnectException;
3939
import java.nio.channels.SocketChannel;
40+
import java.nio.channels.UnresolvedAddressException;
4041
import java.util.Map.Entry;
4142
import java.util.regex.Pattern;
4243
import java.util.*;
@@ -534,6 +535,8 @@ public static void connect(Socket socket,
534535
}
535536
} catch (SocketTimeoutException ste) {
536537
throw new ConnectTimeoutException(ste.getMessage());
538+
} catch (UnresolvedAddressException uae) {
539+
throw new UnknownHostException(uae.getMessage());
537540
}
538541

539542
// There is a very rare case allowed by the TCP specification, such that

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,25 @@ public void testAvoidLoopbackTcpSockets() throws Throwable {
9595
assertInException(se, "Invalid argument");
9696
}
9797
}
98-
98+
99+
@Test
100+
public void testInvalidAddress() throws Throwable {
101+
Configuration conf = new Configuration();
102+
103+
Socket socket = NetUtils.getDefaultSocketFactory(conf)
104+
.createSocket();
105+
socket.bind(new InetSocketAddress("127.0.0.1", 0));
106+
try {
107+
NetUtils.connect(socket,
108+
new InetSocketAddress("invalid-test-host",
109+
0), 20000);
110+
socket.close();
111+
fail("Should not have connected");
112+
} catch (UnknownHostException uhe) {
113+
LOG.info("Got exception: ", uhe);
114+
}
115+
}
116+
99117
@Test
100118
public void testSocketReadTimeoutWithChannel() throws Exception {
101119
doSocketReadTimeoutTest(true);

0 commit comments

Comments
 (0)