Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static void doClientAuth(ClientAuthHandler authHandler, FlightServiceStub

private static class AuthObserver implements StreamObserver<HandshakeResponse> {

// 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<HandshakeRequest> responseObserver;
private final LinkedBlockingQueue<byte[]> messages = new LinkedBlockingQueue<>();
private final AuthSender sender = new AuthSender();
Expand All @@ -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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When take() is interrupted, this exception exits doClientAuth before the outbound handshake stream is completed or cancelled. The server can remain blocked waiting for a client message.
I would signal an error on the request stream before propagating the interruption.

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
Expand All @@ -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 {
Expand All @@ -149,6 +156,7 @@ public void onError(Throwable cause) {
@Override
public void onCompleted() {
completed.complete(true);
messages.add(END_OF_STREAM);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public static StreamObserver<HandshakeRequest> wrapHandshake(

private static class AuthObserver implements StreamObserver<HandshakeRequest> {

// 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<HandshakeResponse> responseObserver;
private volatile Future<?> future;
private volatile boolean completed = false;
private final LinkedBlockingQueue<byte[]> messages = new LinkedBlockingQueue<>();
private final AuthSender sender = new AuthSender();

Expand All @@ -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 */
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<HandshakeResponse>() {
@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<Thread, StackTraceElement[]> 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();
Expand Down
Loading