From 26fba8701c97928bb2ed2e6b456ab5ba9513e0fe Mon Sep 17 00:00:00 2001 From: Owen O'Malley Date: Fri, 10 Feb 2023 09:51:03 -0800 Subject: [PATCH] HDFS-18324. Fix race condition in closing IPC connections. (#5371) --- .../src/main/java/org/apache/hadoop/ipc/Client.java | 9 ++++++++- .../src/test/java/org/apache/hadoop/ipc/TestIPC.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java index c0f90d98bc..c52af06c5a 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java @@ -1181,7 +1181,14 @@ public void sendRpcRequest(final Call call) final ResponseBuffer buf = new ResponseBuffer(); header.writeDelimitedTo(buf); RpcWritable.wrap(call.rpcRequest).writeTo(buf); - rpcRequestQueue.put(Pair.of(call, buf)); + // Wait for the message to be sent. We offer with timeout to + // prevent a race condition between checking the shouldCloseConnection + // and the stopping of the polling thread + while (!shouldCloseConnection.get()) { + if (rpcRequestQueue.offer(Pair.of(call, buf), 1, TimeUnit.SECONDS)) { + break; + } + } } /* Receive a response. diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java index 25c6976549..b65f86a0f7 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java @@ -1336,7 +1336,7 @@ interface DummyProtocol { /** * Test the retry count while used in a retry proxy. */ - @Test(timeout=60000) + @Test(timeout=100000) public void testRetryProxy() throws IOException { final Client client = new Client(LongWritable.class, conf);