From 8987a3d9ad3788dddbaeb2d737ff96e60bfb57f8 Mon Sep 17 00:00:00 2001 From: abdul rawoof Date: Sat, 14 Nov 2026 01:17:59 +0530 Subject: [PATCH 1/2] MINOR: Block instead of spinning while waiting for Flight handshake messages The legacy handshake iterators busy-waited on the message queue, so an idle Handshake stream kept a server executor thread at full CPU before authentication. Block on the queue and wake the reader with an end-of-stream marker instead. --- .../arrow/flight/auth/ClientAuthWrapper.java | 26 +++++---- .../arrow/flight/auth/ServerAuthWrapper.java | 30 ++++++----- .../arrow/flight/auth/TestBasicAuth.java | 53 +++++++++++++++++++ 3 files changed, 88 insertions(+), 21 deletions(-) diff --git a/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java b/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java index 19d4f77a03..e148cfeb80 100644 --- a/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java +++ b/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java @@ -63,6 +63,9 @@ public static void doClientAuth(ClientAuthHandler authHandler, FlightServiceStub private static class AuthObserver implements StreamObserver { + // Queued after the last message so a blocked reader wakes up when the server is done. + private static final byte[] END_OF_STREAM = new byte[0]; + private volatile StreamObserver responseObserver; private final LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); private final AuthSender sender = new AuthSender(); @@ -84,15 +87,17 @@ public void onNext(HandshakeResponse value) { @Override public byte[] next() { - while (!completed.isDone() || !messages.isEmpty()) { - byte[] bytes = messages.poll(); - if (bytes == null) { - // busy wait. - continue; - } else { - return bytes; - } + final byte[] bytes; + try { + bytes = messages.take(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + if (bytes != END_OF_STREAM) { + return bytes; } + messages.add(END_OF_STREAM); if (completed.isCompletedExceptionally()) { // Preserve prior exception behavior @@ -116,13 +121,15 @@ public byte[] next() { @Override public boolean hasNext() { - return !messages.isEmpty(); + final byte[] head = messages.peek(); + return head != null && head != END_OF_STREAM; } }; @Override public void onError(Throwable t) { completed.completeExceptionally(t); + messages.add(END_OF_STREAM); } private class AuthSender implements ClientAuthSender { @@ -149,6 +156,7 @@ public void onError(Throwable cause) { @Override public void onCompleted() { completed.complete(true); + messages.add(END_OF_STREAM); } } } diff --git a/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java b/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java index fba9f9baba..73a560a9dc 100644 --- a/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java +++ b/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java @@ -70,9 +70,11 @@ public static StreamObserver wrapHandshake( private static class AuthObserver implements StreamObserver { + // Queued after the last message so a blocked reader wakes up when the client is done. + private static final byte[] END_OF_STREAM = new byte[0]; + private final StreamObserver responseObserver; private volatile Future future; - private volatile boolean completed = false; private final LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); private final AuthSender sender = new AuthSender(); @@ -92,26 +94,30 @@ public void onNext(HandshakeRequest value) { @Override public byte[] next() { - while (!completed || !messages.isEmpty()) { - byte[] bytes = messages.poll(); - if (bytes == null) { - // busy wait. - continue; - } - return bytes; + final byte[] bytes; + try { + bytes = messages.take(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted while waiting for a message.", e); + } + if (bytes == END_OF_STREAM) { + messages.add(END_OF_STREAM); + throw new IllegalStateException("Requesting more messages than client sent."); } - throw new IllegalStateException("Requesting more messages than client sent."); + return bytes; } @Override public boolean hasNext() { - return !messages.isEmpty(); + final byte[] head = messages.peek(); + return head != null && head != END_OF_STREAM; } }; @Override public void onError(Throwable t) { - completed = true; + messages.add(END_OF_STREAM); while (future == null) { /* busy wait */ } @@ -120,7 +126,7 @@ public void onError(Throwable t) { @Override public void onCompleted() { - completed = true; + messages.add(END_OF_STREAM); } private class AuthSender implements ServerAuthSender { diff --git a/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java b/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java index 0f202ba2d9..851723780c 100644 --- a/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java +++ b/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java @@ -19,13 +19,19 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.fail; import com.google.common.collect.ImmutableList; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.stub.StreamObserver; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.Map; import java.util.Optional; +import java.util.concurrent.TimeUnit; import org.apache.arrow.flight.Criteria; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; @@ -35,6 +41,8 @@ import org.apache.arrow.flight.FlightTestUtil; import org.apache.arrow.flight.NoOpFlightProducer; import org.apache.arrow.flight.Ticket; +import org.apache.arrow.flight.impl.Flight.HandshakeResponse; +import org.apache.arrow.flight.impl.FlightServiceGrpc; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.AutoCloseables; @@ -99,6 +107,51 @@ public void didntAuth() { }); } + @Test + public void handshakeBlocksWhileWaitingForClient() throws Exception { + final ManagedChannel channel = + ManagedChannelBuilder.forAddress(LOCALHOST, server.getPort()).usePlaintext().build(); + try { + // Open a handshake but never send a message, so the server waits for the first one. + FlightServiceGrpc.newStub(channel) + .handshake( + new StreamObserver() { + @Override + public void onNext(HandshakeResponse value) {} + + @Override + public void onError(Throwable t) {} + + @Override + public void onCompleted() {} + }); + + Thread handshakeThread = null; + for (int i = 0; i < 100 && handshakeThread == null; i++) { + Thread.sleep(50); + handshakeThread = findServerHandshakeThread(); + } + assertNotNull(handshakeThread, "server handshake did not start"); + Thread.sleep(200); + // The thread must park until a message arrives rather than spin on the queue. + assertEquals(Thread.State.WAITING, handshakeThread.getState()); + } finally { + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + } + + private static Thread findServerHandshakeThread() { + for (Map.Entry entry : Thread.getAllStackTraces().entrySet()) { + for (StackTraceElement frame : entry.getValue()) { + if (frame.getClassName().startsWith(ServerAuthWrapper.class.getName()) + && frame.getMethodName().equals("next")) { + return entry.getKey(); + } + } + } + return null; + } + @BeforeEach public void testSetup() throws IOException { client = FlightClient.builder(allocator, server.getLocation()).build(); From ae5edcb8c08742e46ee7931301374fb8c08b4cd2 Mon Sep 17 00:00:00 2001 From: abdul rawoof Date: Thu, 26 Nov 2026 13:13:24 +0530 Subject: [PATCH 2/2] MINOR: Error the handshake request stream when the client reader is interrupted An interrupt in next() left doClientAuth without completing or cancelling the outbound stream, so the server could stay blocked waiting for a client message. Signal the error on the request stream before propagating the interruption. --- .../arrow/flight/auth/ClientAuthWrapper.java | 2 + .../arrow/flight/auth/TestBasicAuth.java | 76 ++++++++++++++++--- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java b/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java index e148cfeb80..d3df09e6c9 100644 --- a/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java +++ b/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java @@ -92,6 +92,8 @@ public byte[] next() { bytes = messages.take(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); + // Fail the request stream, otherwise the server keeps waiting for a message from us. + sender.onError(e); throw new RuntimeException(e); } if (bytes != END_OF_STREAM) { diff --git a/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java b/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java index 851723780c..61dd5f8cef 100644 --- a/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java +++ b/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java @@ -19,7 +19,9 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import com.google.common.collect.ImmutableList; @@ -29,9 +31,11 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.Iterator; import java.util.Map; import java.util.Optional; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import org.apache.arrow.flight.Criteria; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; @@ -126,11 +130,7 @@ public void onError(Throwable t) {} public void onCompleted() {} }); - Thread handshakeThread = null; - for (int i = 0; i < 100 && handshakeThread == null; i++) { - Thread.sleep(50); - handshakeThread = findServerHandshakeThread(); - } + final Thread handshakeThread = awaitServerHandshakeThread(); assertNotNull(handshakeThread, "server handshake did not start"); Thread.sleep(200); // The thread must park until a message arrives rather than spin on the queue. @@ -140,18 +140,74 @@ public void onCompleted() {} } } - private static Thread findServerHandshakeThread() { - for (Map.Entry entry : Thread.getAllStackTraces().entrySet()) { - for (StackTraceElement frame : entry.getValue()) { - if (frame.getClassName().startsWith(ServerAuthWrapper.class.getName()) - && frame.getMethodName().equals("next")) { + @Test + public void interruptedClientHandshakeEndsServerStream() throws Exception { + final AtomicReference serverThread = new AtomicReference<>(); + try { + assertThrows( + RuntimeException.class, + () -> + client.authenticate( + new ClientAuthHandler() { + @Override + public void authenticate(ClientAuthSender outgoing, Iterator incoming) { + // Let the server park on our first message, then stop mid-handshake. + serverThread.set(awaitServerHandshakeThread()); + Thread.currentThread().interrupt(); + incoming.next(); + } + + @Override + public byte[] getCallToken() { + return VALID_TOKEN; + } + })); + } finally { + // next() re-raises the flag before propagating, so clear it for the rest of the suite. + Thread.interrupted(); + } + assertNotNull(serverThread.get(), "server handshake did not start"); + + // The interrupted client errors the request stream, so the server gives up on the message. + for (int i = 0; i < 100 && isWaitingForHandshakeMessage(serverThread.get()); i++) { + Thread.sleep(50); + } + assertFalse( + isWaitingForHandshakeMessage(serverThread.get()), + "server handshake stayed blocked after the client was interrupted"); + } + + private static Thread awaitServerHandshakeThread() { + for (int i = 0; i < 100; i++) { + for (Map.Entry entry : Thread.getAllStackTraces().entrySet()) { + if (isWaitingForHandshakeMessage(entry.getValue())) { return entry.getKey(); } } + try { + Thread.sleep(50); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } } return null; } + private static boolean isWaitingForHandshakeMessage(Thread thread) { + return isWaitingForHandshakeMessage(thread.getStackTrace()); + } + + private static boolean isWaitingForHandshakeMessage(StackTraceElement[] stack) { + for (StackTraceElement frame : stack) { + if (frame.getClassName().startsWith(ServerAuthWrapper.class.getName()) + && frame.getMethodName().equals("next")) { + return true; + } + } + return false; + } + @BeforeEach public void testSetup() throws IOException { client = FlightClient.builder(allocator, server.getLocation()).build();